pmodules.pas 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses fmodule;
  21. function proc_unit(curr: tmodule):boolean;
  22. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  23. function proc_unit_implementation(curr: tmodule):boolean;
  24. function proc_package(curr: tmodule) : boolean;
  25. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  26. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  27. function finish_compile_unit(module:tmodule): boolean;
  28. function finish_unit(module:tmodule): boolean;
  29. implementation
  30. uses
  31. SysUtils,
  32. globtype,systems,tokens,
  33. cutils,cfileutl,cclasses,comphook,
  34. globals,verbose,finput,fppu,globstat,fpcp,fpkg,
  35. symconst,symbase,symtype,symdef,symsym,symtable,defutil,symcreat,
  36. wpoinfo,
  37. aasmtai,aasmdata,aasmbase,aasmcpu,
  38. cgbase,ngenutil,
  39. nbas,nutils,ncgutil,
  40. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  41. cresstr,procinfo,
  42. objcgutl,
  43. pkgutil,
  44. wpobase,
  45. scanner,pbase,pexpr,psystem,psub,pgenutil,pparautl,ncgvmt,ncgrtti,
  46. ctask,
  47. cpuinfo;
  48. procedure create_objectfile(curr : tmodule);
  49. var
  50. DLLScanner : TDLLScanner;
  51. s : string;
  52. KeepShared : TCmdStrList;
  53. begin
  54. { try to create import entries from system dlls }
  55. if (tf_has_dllscanner in target_info.flags) and
  56. (not curr.linkOtherSharedLibs.Empty) then
  57. begin
  58. { Init DLLScanner }
  59. if assigned(CDLLScanner[target_info.system]) then
  60. DLLScanner:=CDLLScanner[target_info.system].Create
  61. else
  62. internalerror(200104121);
  63. KeepShared:=TCmdStrList.Create;
  64. { Walk all shared libs }
  65. While not curr.linkOtherSharedLibs.Empty do
  66. begin
  67. S:=curr.linkOtherSharedLibs.Getusemask(link_always);
  68. if not DLLScanner.scan(s) then
  69. KeepShared.Concat(s);
  70. end;
  71. DLLscanner.Free;
  72. DLLscanner := nil;
  73. { Recreate import section }
  74. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  75. begin
  76. if assigned(current_asmdata.asmlists[al_imports]) then
  77. current_asmdata.asmlists[al_imports].clear
  78. else
  79. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  80. importlib.generatelib;
  81. end;
  82. { Readd the not processed files }
  83. while not KeepShared.Empty do
  84. begin
  85. s:=KeepShared.GetFirst;
  86. curr.linkOtherSharedLibs.add(s,link_always);
  87. end;
  88. KeepShared.Free;
  89. KeepShared := nil;
  90. end;
  91. { allow a target-specific pass over all assembler code (used by LLVM
  92. to insert type definitions }
  93. cnodeutils.InsertObjectInfo;
  94. { Start and end module debuginfo, at least required for stabs
  95. to insert n_sourcefile lines }
  96. if (cs_debuginfo in current_settings.moduleswitches) or
  97. (cs_use_lineinfo in current_settings.globalswitches) then
  98. current_debuginfo.insertmoduleinfo;
  99. { create the .s file and assemble it }
  100. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  101. GenerateAsm(false);
  102. { Also create a smartlinked version ? }
  103. if create_smartlink_library then
  104. begin
  105. GenerateAsm(true);
  106. if (af_needar in target_asm.flags) then
  107. Linker.MakeStaticLibrary;
  108. end;
  109. { resource files }
  110. CompileResourceFiles;
  111. end;
  112. procedure insertobjectfile(curr : tmodule);
  113. { Insert the used object file for this unit in the used list for this unit }
  114. begin
  115. curr.linkunitofiles.add(curr.objfilename,link_static);
  116. curr.headerflags:=curr.headerflags or uf_static_linked;
  117. if create_smartlink_library then
  118. begin
  119. curr.linkunitstaticlibs.add(curr.staticlibfilename ,link_smart);
  120. curr.headerflags:=curr.headerflags or uf_smart_linked;
  121. end;
  122. if cs_lto in current_settings.moduleswitches then
  123. begin
  124. curr.linkunitofiles.add(ChangeFileExt(curr.objfilename,LTOExt),link_lto);
  125. curr.headerflags:=curr.headerflags or uf_lto_linked;
  126. end;
  127. end;
  128. procedure create_dwarf_frame;
  129. begin
  130. { Dwarf conflicts with smartlinking in separate .a files }
  131. if create_smartlink_library then
  132. exit;
  133. { Call frame information }
  134. { MWE: we write our own info, so dwarf asm support is not really needed }
  135. { if (af_supports_dwarf in target_asm.flags) and }
  136. { CFI is currently broken for Darwin }
  137. if not(target_info.system in systems_darwin) and
  138. (
  139. (tf_needs_dwarf_cfi in target_info.flags) or
  140. (target_dbg.id in [dbg_dwarf2, dbg_dwarf3])
  141. ) then
  142. begin
  143. current_asmdata.asmlists[al_dwarf_frame].Free;
  144. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  145. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  146. end;
  147. end;
  148. Function CheckResourcesUsed(curr : tmodule) : boolean;
  149. var
  150. hp : tused_unit;
  151. found : Boolean;
  152. begin
  153. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  154. if not CheckResourcesUsed then exit;
  155. hp:=tused_unit(usedunits.first);
  156. found:=mf_has_resourcefiles in curr.moduleflags;
  157. while Assigned(hp) and not found do
  158. begin
  159. found:=mf_has_resourcefiles in hp.u.moduleflags;
  160. hp:=tused_unit(hp.next);
  161. end;
  162. CheckResourcesUsed:=found;
  163. end;
  164. function AddUnit(curr : tmodule; const s:string;addasused:boolean): tppumodule;
  165. var
  166. hp : tppumodule;
  167. unitsym : tunitsym;
  168. isnew,load_ok : boolean;
  169. begin
  170. { load unit }
  171. hp:=registerunit(curr,s,'',isnew);
  172. if isnew then
  173. usedunits.concat(tused_unit.create(hp,true,addasused,nil));
  174. load_ok:=hp.loadppu(curr);
  175. hp.adddependency(curr,curr.in_interface);
  176. if not load_ok then
  177. { We must schedule a compile. }
  178. task_handler.addmodule(hp);
  179. { add to symtable stack }
  180. if assigned(hp.globalsymtable) then
  181. symtablestack.push(hp.globalsymtable);
  182. if (m_mac in current_settings.modeswitches) and
  183. assigned(hp.globalmacrosymtable) then
  184. macrosymtablestack.push(hp.globalmacrosymtable);
  185. { insert unitsym }
  186. unitsym:=cunitsym.create(hp.modulename^,hp);
  187. inc(unitsym.refs);
  188. tabstractunitsymtable(curr.localsymtable).insertunit(unitsym);
  189. if addasused then
  190. { add to used units }
  191. curr.addusedunit(hp,false,unitsym);
  192. result:=hp;
  193. end;
  194. function AddUnit(curr :tmodule; const s:string):tppumodule;
  195. begin
  196. result:=AddUnit(curr,s,true);
  197. end;
  198. function maybeloadvariantsunit(curr : tmodule) : boolean;
  199. var
  200. hp : tmodule;
  201. addsystemnamespace : Boolean;
  202. begin
  203. result:=true;
  204. { Do we need the variants unit? Skip this
  205. for VarUtils unit for bootstrapping }
  206. if not(mf_uses_variants in curr.moduleflags) or
  207. (curr.modulename^='VARUTILS') or
  208. (curr.modulename^='SYSTEM.VARUTILS') then
  209. exit;
  210. { Variants unit already loaded? }
  211. hp:=tmodule(loaded_units.first);
  212. while assigned(hp) do
  213. begin
  214. if (hp.modulename^='VARIANTS') or (hp.modulename^='SYSTEM.VARIANTS') then
  215. exit;
  216. hp:=tmodule(hp.next);
  217. end;
  218. { Variants unit is not loaded yet, load it now }
  219. Message(parser_w_implicit_uses_of_variants_unit);
  220. addsystemnamespace:=namespacelist.Find('System')=Nil;
  221. if addsystemnamespace then
  222. namespacelist.concat('System');
  223. result:=AddUnit(curr,'variants').state in [ms_compiled,ms_processed];
  224. if addsystemnamespace then
  225. namespacelist.Remove('System');
  226. end;
  227. function MaybeRemoveResUnit(curr : tmodule) : boolean;
  228. var
  229. resources_used : boolean;
  230. hp : tmodule;
  231. uu : tused_unit;
  232. unitname : shortstring;
  233. begin
  234. { We simply remove the unit from:
  235. - usedunit list, so that things like init/finalization table won't
  236. contain references to this unit
  237. - loaded_units list, so that the unit object file doesn't get linked
  238. with the executable. }
  239. { Note: on windows we always need resources! }
  240. resources_used:=(target_info.system in systems_all_windows)
  241. or CheckResourcesUsed(curr);
  242. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  243. begin
  244. { resources aren't used, so we don't need this unit }
  245. if target_res.id=res_ext then
  246. unitname:='FPEXTRES'
  247. else
  248. unitname:='FPINTRES';
  249. Message1(unit_u_unload_resunit,unitname);
  250. { find the module }
  251. hp:=tmodule(loaded_units.first);
  252. while assigned(hp) do
  253. begin
  254. if hp.is_unit and (hp.modulename^=unitname) then break;
  255. hp:=tmodule(hp.next);
  256. end;
  257. if not assigned(hp) then
  258. internalerror(200801071);
  259. { find its tused_unit in the global list }
  260. uu:=tused_unit(usedunits.first);
  261. while assigned(uu) do
  262. begin
  263. if uu.u=hp then break;
  264. uu:=tused_unit(uu.next);
  265. end;
  266. if not assigned(uu) then
  267. internalerror(200801072);
  268. { remove the tused_unit }
  269. usedunits.Remove(uu);
  270. uu.Free;
  271. // Remove from local list
  272. uu:=tused_unit(curr.used_units.first);
  273. while assigned(uu) do
  274. begin
  275. if uu.u=hp then break;
  276. uu:=tused_unit(uu.next);
  277. end;
  278. if not assigned(uu) then
  279. internalerror(2024020701);
  280. curr.used_units.Remove(uu);
  281. uu.Free;
  282. uu := nil;
  283. { remove the module }
  284. loaded_units.Remove(hp);
  285. unloaded_units.Concat(hp);
  286. end;
  287. MaybeRemoveResUnit:=resources_used;
  288. end;
  289. function loadsystemunit(curr : tmodule) : boolean;
  290. var
  291. state: tglobalstate;
  292. sys : tmodule;
  293. begin
  294. Result:=False;
  295. { we are going to rebuild the symtablestack, clear it first }
  296. symtablestack.clear;
  297. macrosymtablestack.clear;
  298. { macro symtable }
  299. macrosymtablestack.push(initialmacrosymtable);
  300. { are we compiling the system unit? }
  301. if (cs_compilesystem in current_settings.moduleswitches) then
  302. begin
  303. systemunit:=tglobalsymtable(curr.localsymtable);
  304. { create system defines }
  305. create_intern_types;
  306. create_intern_symbols;
  307. { Set the owner of errorsym and errortype to symtable to
  308. prevent crashes when accessing .owner }
  309. generrorsym.owner:=systemunit;
  310. generrordef.owner:=systemunit;
  311. exit;
  312. end;
  313. { insert the system unit, it is allways the first. Load also the
  314. internal types from the system unit }
  315. Sys:=AddUnit(curr,'system');
  316. Result:=Assigned(Sys) and (Sys.State in [ms_processed,ms_compiled]);
  317. systemunit:=tglobalsymtable(symtablestack.top);
  318. { load_intern_types resets the scanner... }
  319. current_scanner.tempcloseinputfile;
  320. state:=tglobalstate.create(true);
  321. load_intern_types;
  322. state.restore(true);
  323. FreeAndNil(state);
  324. current_scanner.tempopeninputfile;
  325. { Set the owner of errorsym and errortype to symtable to
  326. prevent crashes when accessing .owner }
  327. generrorsym.owner:=systemunit;
  328. generrordef.owner:=systemunit;
  329. // Implicitly enable unicode strings in unicode RTL in modes objfpc/delphi.
  330. { TODO: Check if we should also do this for mode macpas }
  331. if not (cs_compilesystem in current_settings.moduleswitches) then
  332. if ([m_objfpc,m_delphi] * current_settings.modeswitches)<>[] then
  333. if is_systemunit_unicode then
  334. Include(current_settings.modeswitches,m_default_unicodestring);
  335. { default the extended RTTI options to that of TObject }
  336. if assigned(class_tobject) then
  337. current_module.rtti_directive.options:=class_tobject.rtti.options;
  338. end;
  339. { Return true if all units were loaded, no recompilation needed. }
  340. function loaddefaultunits(curr :tmodule) : boolean;
  341. Procedure CheckAddUnit(s: string);
  342. var
  343. OK : boolean;
  344. m : TModule;
  345. begin
  346. m:=AddUnit(curr,s,true);
  347. OK:=assigned(m) and (m.state in [ms_processed,ms_compiled]);
  348. if not ok then
  349. Message2(unit_f_cant_find_ppu,s,curr.realmodulename^);
  350. Result:=ok and Result;
  351. end;
  352. begin
  353. Result:=True;
  354. { Units only required for main module }
  355. if not(curr.is_unit) then
  356. begin
  357. { Heaptrc unit, load heaptrace before any other units especially objpas }
  358. if (cs_use_heaptrc in current_settings.globalswitches) then
  359. CheckAddUnit('heaptrc');
  360. { Valgrind requires c memory manager }
  361. if (cs_gdb_valgrind in current_settings.globalswitches) or
  362. (([cs_sanitize_address]*current_settings.moduleswitches)<>[]) then
  363. CheckAddUnit('cmem');
  364. { Lineinfo unit }
  365. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  366. case target_dbg.id of
  367. dbg_stabs:
  368. CheckAddUnit('lineinfo');
  369. dbg_stabx:
  370. CheckAddUnit('lnfogdb');
  371. else
  372. CheckAddUnit('lnfodwrf');
  373. end;
  374. end;
  375. {$ifdef cpufpemu}
  376. { Floating point emulation unit?
  377. softfpu must be in the system unit anyways (FK)
  378. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  379. CheckAddUnit('softfpu');
  380. }
  381. {$endif cpufpemu}
  382. { Which kind of resource support?
  383. Note: if resources aren't used this unit will be removed later,
  384. otherwise we need it here since it must be loaded quite early }
  385. if (tf_has_winlike_resources in target_info.flags) then
  386. if target_res.id=res_ext then
  387. CheckAddUnit('fpextres')
  388. else
  389. CheckAddUnit('fpintres');
  390. end
  391. else if (cs_checkpointer in current_settings.localswitches) then
  392. CheckAddUnit('heaptrc');
  393. { Objpas unit? }
  394. if m_objpas in current_settings.modeswitches then
  395. CheckAddUnit('objpas');
  396. { Macpas unit? }
  397. if m_mac in current_settings.modeswitches then
  398. CheckAddUnit('macpas');
  399. if m_iso in current_settings.modeswitches then
  400. CheckAddUnit('iso7185');
  401. if m_extpas in current_settings.modeswitches then
  402. begin
  403. { basic procedures for Extended Pascal are for now provided by the iso unit }
  404. CheckAddUnit('iso7185');
  405. CheckAddUnit('extpas');
  406. end;
  407. { blocks support? }
  408. if m_blocks in current_settings.modeswitches then
  409. CheckAddUnit('blockrtl');
  410. { Determine char size. }
  411. // Ansi RTL ?
  412. if not is_systemunit_unicode then
  413. begin
  414. if m_default_unicodestring in current_settings.modeswitches then
  415. CheckAddUnit('uuchar'); // redefines char as widechar
  416. end
  417. else
  418. begin
  419. // Unicode RTL
  420. if not (m_default_ansistring in current_settings.modeswitches) then
  421. if not (curr.modulename^<>'UACHAR') then
  422. CheckAddUnit('uachar'); // redefines char as ansichar
  423. end;
  424. { Objective-C support unit? }
  425. if (m_objectivec1 in current_settings.modeswitches) then
  426. begin
  427. { interface to Objective-C run time }
  428. CheckAddUnit('objc');
  429. loadobjctypes;
  430. { NSObject }
  431. if not(curr.is_unit) or
  432. (curr.modulename^<>'OBJCBASE') then
  433. CheckAddUnit('objcbase');
  434. end;
  435. { Profile unit? Needed for go32v2 only }
  436. if (cs_profile in current_settings.moduleswitches) and
  437. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  438. CheckAddUnit('profile');
  439. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  440. begin
  441. CheckAddUnit('fpcylix');
  442. CheckAddUnit('dynlibs');
  443. end;
  444. {$push}
  445. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  446. { CPU targets with microcontroller support can add a controller specific unit }
  447. if ControllerSupport and (target_info.system in (systems_embedded+systems_freertos)) and
  448. (current_settings.controllertype<>ct_none) and
  449. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') and
  450. (embedded_controllers[current_settings.controllertype].controllerunitstr<>curr.modulename^) then
  451. CheckAddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  452. {$pop}
  453. {$ifdef XTENSA}
  454. if not(curr.is_unit) and (target_info.system=system_xtensa_freertos) then
  455. if (current_settings.controllertype=ct_esp32) then
  456. begin
  457. if (idf_version>=40100) and (idf_version<40200) then
  458. CheckAddUnit('espidf_40100')
  459. else if (idf_version>=40200) and (idf_version<40400) then
  460. CheckAddUnit('espidf_40200')
  461. else if (idf_version>=40400) and (idf_version<50000) then
  462. CheckAddUnit('espidf_40400')
  463. else if (idf_version>=50000) and (idf_version<50200) then
  464. CheckAddUnit('espidf_50000')
  465. else if idf_version>=50200 then
  466. CheckAddUnit('espidf_50200')
  467. else
  468. Comment(V_Warning, 'Unsupported esp-idf version');
  469. end
  470. else if (current_settings.controllertype=ct_esp32s2) or (current_settings.controllertype=ct_esp32s3) then
  471. begin
  472. if (idf_version>=40400) and (idf_version<50000) then
  473. CheckAddUnit('espidf_40400')
  474. else if (idf_version>=50000) and (idf_version<50200) then
  475. CheckAddUnit('espidf_50000')
  476. else if idf_version>=50200 then
  477. CheckAddUnit('espidf_50200')
  478. else
  479. Message(unit_w_unsupported_esp_idf_version);
  480. end
  481. else if (current_settings.controllertype=ct_esp8266) then
  482. begin
  483. if (idf_version>=30300) and (idf_version<30400) then
  484. CheckAddUnit('esp8266rtos_30300')
  485. else if idf_version>=30400 then
  486. CheckAddUnit('esp8266rtos_30400')
  487. else
  488. Message(unit_w_unsupported_esp_idf_version);
  489. end;
  490. {$endif XTENSA}
  491. {$ifdef RISCV32}
  492. if not(curr.is_unit) and (target_info.system=system_riscv32_freertos) then
  493. if (current_settings.controllertype=ct_esp32c2) then
  494. begin
  495. if idf_version>=50200 then
  496. CheckAddUnit('esp32c2idf_50200')
  497. else if idf_version>=50000 then
  498. CheckAddUnit('esp32c2idf_50000')
  499. else if idf_version>=40400 then
  500. CheckAddUnit('esp32c2idf_40400')
  501. else
  502. Comment(V_Warning, 'Unsupported esp-idf version');
  503. end;
  504. if (current_settings.controllertype=ct_esp32c3) then
  505. begin
  506. if idf_version>=50300 then
  507. CheckAddUnit('esp32c3idf_50300')
  508. else if idf_version>=50200 then
  509. CheckAddUnit('esp32c3idf_50200')
  510. else if idf_version>=50000 then
  511. CheckAddUnit('esp32c3idf_50000')
  512. else if idf_version>=40400 then
  513. CheckAddUnit('esp32c3idf_40400')
  514. else
  515. Message(unit_w_unsupported_esp_idf_version);
  516. end;
  517. if (current_settings.controllertype=ct_esp32c6) then
  518. begin
  519. if idf_version>=50200 then
  520. CheckAddUnit('esp32c6idf_50200')
  521. else
  522. Comment(V_Warning, 'Unsupported esp-idf version');
  523. end;
  524. {$endif RISCV32}
  525. end;
  526. { Return true if all units were loaded, no recompilation needed. }
  527. function loadautounits(curr: tmodule) : boolean;
  528. Procedure CheckAddUnit(s: string);
  529. var
  530. OK : boolean;
  531. m : TModule;
  532. begin
  533. m:=AddUnit(curr,s,true);
  534. OK:=assigned(m) and (m.state in [ms_compiled,ms_processed]);
  535. Result:=ok and Result;
  536. end;
  537. var
  538. hs,s : string;
  539. begin
  540. Result:=True;
  541. hs:=autoloadunits;
  542. repeat
  543. s:=GetToken(hs,',');
  544. if s='' then
  545. break;
  546. CheckAddUnit(s);
  547. until false;
  548. end;
  549. procedure parseusesclause(curr: tmodule);
  550. var
  551. s,sorg : ansistring;
  552. fn : string;
  553. pu : tused_unit;
  554. hp2 : tmodule;
  555. unitsym : tunitsym;
  556. filepos : tfileposinfo;
  557. isnew : boolean;
  558. begin
  559. consume(_USES);
  560. repeat
  561. s:=pattern;
  562. sorg:=orgpattern;
  563. filepos:=current_tokenpos;
  564. consume(_ID);
  565. while token=_POINT do
  566. begin
  567. consume(_POINT);
  568. s:=s+'.'+pattern;
  569. sorg:=sorg+'.'+orgpattern;
  570. consume(_ID);
  571. end;
  572. { support "<unit> in '<file>'" construct, but not for tp7 }
  573. fn:='';
  574. if not(m_tp7 in current_settings.modeswitches) and
  575. try_to_consume(_OP_IN) then
  576. fn:=FixFileName(get_stringconst);
  577. { Give a warning if lineinfo is loaded }
  578. if s='LINEINFO' then
  579. begin
  580. Message(parser_w_no_lineinfo_use_switch);
  581. if (target_dbg.id in [dbg_dwarf2, dbg_dwarf3]) then
  582. s := 'LNFODWRF';
  583. sorg := s;
  584. end;
  585. { Give a warning if objpas is loaded }
  586. if s='OBJPAS' then
  587. Message(parser_w_no_objpas_use_mode);
  588. { Using the unit itself is not possible }
  589. if (s<>curr.modulename^) then
  590. begin
  591. { check if the unit is already used }
  592. hp2:=nil;
  593. pu:=tused_unit(curr.used_units.first);
  594. while assigned(pu) do
  595. begin
  596. if (pu.u.modulename^=s) then
  597. begin
  598. hp2:=pu.u;
  599. break;
  600. end;
  601. pu:=tused_unit(pu.next);
  602. end;
  603. if not assigned(hp2) then
  604. begin
  605. hp2:=registerunit(curr,sorg,fn,isnew);
  606. if isnew then
  607. usedunits.concat(tused_unit.create(hp2,curr.in_interface,true,nil));
  608. end
  609. else
  610. Message1(sym_e_duplicate_id,s);
  611. { Create unitsym, we need to use the name as specified, we
  612. can not use the modulename because that can be different
  613. when -Un is used }
  614. current_tokenpos:=filepos;
  615. unitsym:=cunitsym.create(sorg,nil);
  616. { the current module uses the unit hp2 }
  617. curr.addusedunit(hp2,true,unitsym);
  618. end
  619. else
  620. Message1(sym_e_duplicate_id,s);
  621. if token=_COMMA then
  622. begin
  623. pattern:='';
  624. consume(_COMMA);
  625. end
  626. else
  627. break;
  628. until false;
  629. end;
  630. function loadunits(curr: tmodule; frominterface : boolean) : boolean;
  631. var
  632. s : ansistring;
  633. pu : tused_unit;
  634. state: tglobalstate;
  635. isLoaded : Boolean;
  636. mwait : tmodule;
  637. lu : tmodule;
  638. procedure restorestate;
  639. begin
  640. state.restore(true);
  641. if assigned(current_scanner) and (current_module.scanner=current_scanner) then
  642. begin
  643. if assigned(current_scanner.inputfile) then
  644. current_scanner.tempopeninputfile;
  645. end;
  646. state.free;
  647. state := nil;
  648. end;
  649. begin
  650. Result:=true;
  651. mwait:=nil;
  652. current_scanner.tempcloseinputfile;
  653. state:=tglobalstate.create(true);
  654. { Load the units }
  655. pu:=tused_unit(curr.used_units.first);
  656. while assigned(pu) do
  657. begin
  658. lu:=pu.u;
  659. { Only load the units that are in the current
  660. (interface/implementation) uses clause }
  661. if pu.in_uses and
  662. (pu.in_interface=frominterface) then
  663. begin
  664. if (lu.state in [ms_processed, ms_compiled,ms_compiling_waitimpl]) then
  665. isLoaded:=true
  666. else if (lu.state=ms_registered) then
  667. // try to load
  668. isLoaded:=tppumodule(lu).loadppu(curr)
  669. else
  670. isLoaded:=False;
  671. isLoaded:=IsLoaded and not lu.is_reset ;
  672. if not IsLoaded then
  673. begin
  674. if mwait=nil then
  675. mwait:=lu;
  676. // In case of is_reset, the task handler will discard the state if the module was already there
  677. task_handler.addmodule(lu);
  678. end;
  679. IsLoaded:=Isloaded and not curr.is_reset;
  680. Result:=Result and IsLoaded;
  681. { If we were reset, then used_units is no longer correct, and we must exit at once. }
  682. if curr.is_reset then
  683. break;
  684. { is our module compiled? then we can stop }
  685. if curr.state in [ms_compiled,ms_processed] then
  686. break;
  687. { add this unit to the dependencies }
  688. lu.adddependency(curr,frominterface);
  689. { check hints }
  690. pu.check_hints;
  691. end;
  692. pu:=tused_unit(pu.next);
  693. end;
  694. Restorestate;
  695. end;
  696. {
  697. Connect loaded units: check crc and add to symbol tables.
  698. this can only be called after all units were actually loaded!
  699. }
  700. procedure connect_loaded_units(_module : tmodule; preservest:tsymtable);
  701. var
  702. pu : tused_unit;
  703. sorg : ansistring;
  704. unitsymtable: tabstractunitsymtable;
  705. begin
  706. // writeln(_module.get_modulename,': Connecting units');
  707. pu:=tused_unit(_module.used_units.first);
  708. while assigned(pu) do
  709. begin
  710. {
  711. Writeln('Connect : ',Assigned(_module.modulename), ' ', assigned(pu.u), ' ' ,assigned(pu.u.modulename));
  712. if assigned(pu.u) then
  713. begin
  714. if assigned(pu.u.modulename) then
  715. Writeln(_module.modulename^,': Examining connect of file ',pu._fn,' (',pu.u.modulename^,')')
  716. else
  717. Writeln(_module.modulename^,': Examining connect of file ',pu._fn);
  718. end
  719. else
  720. Writeln(_module.modulename^,': Examining unit without module... ');
  721. }
  722. if not (pu.in_uses and
  723. (pu.in_interface=_module.in_interface)) then
  724. begin
  725. // writeln('Must not connect ',pu.u.modulename^,' (pu.in_interface: ',pu.in_interface,' <> module.in_interface',_module.in_interface,')');
  726. end
  727. else
  728. begin
  729. // writeln('Must connect ',pu.u.modulename^,'(sym: ',pu.unitsym.realname,')');
  730. { save crc values }
  731. pu.checksum:=pu.u.crc;
  732. pu.interface_checksum:=pu.u.interface_crc;
  733. pu.indirect_checksum:=pu.u.indirect_crc;
  734. if tppumodule(pu.u).nsprefix<>'' then
  735. begin
  736. { use the name as declared in the uses section for -Un }
  737. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  738. { update unitsym now that we have access to the full name }
  739. pu.unitsym.free;
  740. pu.unitsym:=cunitsym.create(sorg,pu.u);
  741. end
  742. else
  743. begin
  744. { connect unitsym to the module }
  745. pu.unitsym.module:=pu.u;
  746. pu.unitsym.register_sym;
  747. end;
  748. {
  749. Add the unit symbol in the current symtable.
  750. localsymtable will be nil after the interface uses clause is parsed and the local symtable
  751. is moved to the global.
  752. }
  753. if assigned(_module.localsymtable) then
  754. unitsymtable:=tabstractunitsymtable(_module.localsymtable)
  755. else
  756. unitsymtable:=tabstractunitsymtable(_module.globalsymtable);
  757. // Writeln('Adding used unit sym ',pu.unitsym.realName,' to table ',unitsymtable.get_name);
  758. unitsymtable.insertunit(pu.unitsym);
  759. { add to symtable stack }
  760. // Writeln('Adding used unit symtable ',pu.u.globalsymtable.name^,' (',pu.u.globalsymtable.DefList.Count, ' defs) to stack');
  761. if assigned(preservest) then
  762. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  763. else
  764. symtablestack.push(pu.u.globalsymtable);
  765. if (m_mac in current_settings.modeswitches) and
  766. assigned(pu.u.globalmacrosymtable) then
  767. macrosymtablestack.push(pu.u.globalmacrosymtable);
  768. end;
  769. pu:=tused_unit(pu.next);
  770. end;
  771. // writeln(_module.get_modulename,': Done Connecting units');
  772. end;
  773. procedure reset_all_defs(curr: tmodule);
  774. begin
  775. if assigned(curr.wpoinfo) then
  776. curr.wpoinfo.resetdefs;
  777. end;
  778. procedure free_localsymtables(st:TSymtable);
  779. var
  780. i : longint;
  781. def : tstoreddef;
  782. pd : tprocdef;
  783. begin
  784. for i:=0 to st.DefList.Count-1 do
  785. begin
  786. def:=tstoreddef(st.DefList[i]);
  787. if def.typ=procdef then
  788. begin
  789. pd:=tprocdef(def);
  790. if assigned(pd.localst) and
  791. (pd.localst.symtabletype<>staticsymtable) and
  792. not(po_inline in pd.procoptions) then
  793. begin
  794. free_localsymtables(pd.localst);
  795. pd.localst.free;
  796. pd.localst:=nil;
  797. end;
  798. pd.freeimplprocdefinfo;
  799. pd.done_paraloc_info(calleeside);
  800. end;
  801. end;
  802. end;
  803. procedure free_unregistered_localsymtable_elements(curr : tmodule);
  804. procedure remove_from_procdeflist(adef: tdef);
  805. var
  806. i: Integer;
  807. childdef: tdef;
  808. begin
  809. if adef=nil then exit;
  810. if (adef.typ in [objectdef, recorddef]) and (adef is tabstractrecorddef) then
  811. begin
  812. if tabstractrecorddef(adef).symtable<>nil then
  813. for i:=0 to tabstractrecorddef(adef).symtable.DefList.Count-1 do
  814. begin
  815. childdef:=tdef(tabstractrecorddef(adef).symtable.DefList[i]);
  816. remove_from_procdeflist(childdef);
  817. end;
  818. end
  819. else
  820. if adef.typ=procdef then
  821. begin
  822. tprocsym(tprocdef(adef).procsym).ProcdefList.Remove(adef);
  823. if tprocdef(adef).localst<>nil then
  824. for i:=0 to tprocdef(adef).localst.DefList.Count-1 do
  825. begin
  826. childdef:=tdef(tprocdef(adef).localst.DefList[i]);
  827. remove_from_procdeflist(childdef);
  828. end;
  829. end;
  830. end;
  831. var
  832. i: longint;
  833. def: tdef;
  834. sym: tsym;
  835. begin
  836. for i:=curr.localsymtable.deflist.count-1 downto 0 do
  837. begin
  838. def:=tdef(curr.localsymtable.deflist[i]);
  839. { since commit 48986 deflist might have NIL entries }
  840. if not assigned(def) then
  841. continue;
  842. { this also frees def, as the defs are owned by the symtable }
  843. if not def.is_registered and
  844. not(df_not_registered_no_free in def.defoptions) then
  845. begin
  846. { if it's a procdef, unregister it from its procsym first,
  847. unless that sym hasn't been registered either (it's possible
  848. to have one overload in the interface and another in the
  849. implementation) }
  850. remove_from_procdeflist(def);
  851. curr.localsymtable.deletedef(def);
  852. end;
  853. end;
  854. { from high to low so we hopefully have moves of less data }
  855. for i:=curr.localsymtable.symlist.count-1 downto 0 do
  856. begin
  857. sym:=tsym(curr.localsymtable.symlist[i]);
  858. { this also frees sym, as the symbols are owned by the symtable }
  859. if not sym.is_registered then
  860. curr.localsymtable.DeleteSym(sym);
  861. end;
  862. end;
  863. procedure setupglobalswitches;
  864. begin
  865. if (cs_create_pic in current_settings.moduleswitches) then
  866. begin
  867. def_system_macro('FPC_PIC');
  868. def_system_macro('PIC');
  869. end;
  870. end;
  871. function create_main_proc(const name:TSymStr;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  872. var
  873. ps : tprocsym;
  874. pd : tprocdef;
  875. begin
  876. { there should be no current_procinfo available }
  877. if assigned(current_procinfo) then
  878. internalerror(200304275);
  879. {Generate a procsym for main}
  880. ps:=cprocsym.create('$'+name);
  881. { always register the symbol }
  882. ps.register_sym;
  883. { main are allways used }
  884. inc(ps.refs);
  885. st.insertsym(ps);
  886. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  887. { We don't need a local symtable, change it into the static symtable }
  888. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  889. begin
  890. pd.localst.free;
  891. pd.localst:=st;
  892. end
  893. else if (potype=potype_pkgstub) and
  894. (target_info.system in systems_all_windows+systems_nativent) then
  895. pd.proccalloption:=pocall_stdcall
  896. else
  897. pd.proccalloption:=pocall_cdecl;
  898. handle_calling_convention(pd,hcc_default_actions_impl);
  899. { set procinfo and current_procinfo.procdef }
  900. result:=tcgprocinfo(cprocinfo.create(nil));
  901. result.procdef:=pd;
  902. { main proc does always a call e.g. to init system unit }
  903. if potype<>potype_pkgstub then
  904. include(result.flags,pi_do_call);
  905. end;
  906. procedure release_main_proc(curr: tmodule; pi:tcgprocinfo);
  907. begin
  908. { remove localst as it was replaced by staticsymtable }
  909. pi.procdef.localst:=nil;
  910. { remove procinfo }
  911. curr.procinfo:=nil;
  912. pi.free;
  913. pi:=nil;
  914. end;
  915. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  916. procedure maybe_load_got(curr: tmodule);
  917. {$if defined(i386) or defined (sparcgen)}
  918. var
  919. gotvarsym : tstaticvarsym;
  920. {$endif i386 or sparcgen}
  921. begin
  922. {$if defined(i386) or defined(sparcgen)}
  923. if (cs_create_pic in current_settings.moduleswitches) and
  924. (tf_pic_uses_got in target_info.flags) then
  925. begin
  926. { insert symbol for got access in assembler code}
  927. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  928. vs_value,voidpointertype,[vo_is_external]);
  929. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  930. curr.localsymtable.insertsym(gotvarsym);
  931. { avoid unnecessary warnings }
  932. gotvarsym.varstate:=vs_read;
  933. gotvarsym.refs:=1;
  934. end;
  935. {$endif i386 or sparcgen}
  936. end;
  937. function gen_implicit_initfinal(curr: tmodule; flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  938. begin
  939. { create procdef }
  940. case flag of
  941. mf_init :
  942. begin
  943. result:=create_main_proc(make_mangledname('',curr.localsymtable,'init_implicit$'),potype_unitinit,st);
  944. result.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  945. end;
  946. mf_finalize :
  947. begin
  948. result:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  949. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  950. if (not curr.is_unit) then
  951. result.procdef.aliasnames.concat('PASCALFINALIZE');
  952. end;
  953. else
  954. internalerror(200304253);
  955. end;
  956. result.code:=cnothingnode.create;
  957. end;
  958. procedure copy_macro(p:TObject; arg:pointer);
  959. begin
  960. TModule(arg).globalmacrosymtable.insertsym(tmacro(p).getcopy);
  961. end;
  962. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  963. var
  964. deprecated_seen,
  965. last_is_deprecated:boolean;
  966. begin
  967. try_consume_hintdirective:=false;
  968. deprecated_seen:=false;
  969. repeat
  970. last_is_deprecated:=false;
  971. case idtoken of
  972. _LIBRARY :
  973. begin
  974. include(moduleopt,mo_hint_library);
  975. try_consume_hintdirective:=true;
  976. end;
  977. _DEPRECATED :
  978. begin
  979. { allow deprecated only once }
  980. if deprecated_seen then
  981. break;
  982. include(moduleopt,mo_hint_deprecated);
  983. try_consume_hintdirective:=true;
  984. last_is_deprecated:=true;
  985. deprecated_seen:=true;
  986. end;
  987. _EXPERIMENTAL :
  988. begin
  989. include(moduleopt,mo_hint_experimental);
  990. try_consume_hintdirective:=true;
  991. end;
  992. _PLATFORM :
  993. begin
  994. include(moduleopt,mo_hint_platform);
  995. try_consume_hintdirective:=true;
  996. end;
  997. _UNIMPLEMENTED :
  998. begin
  999. include(moduleopt,mo_hint_unimplemented);
  1000. try_consume_hintdirective:=true;
  1001. end;
  1002. else
  1003. break;
  1004. end;
  1005. consume(Token);
  1006. { handle deprecated message }
  1007. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  1008. begin
  1009. if deprecatedmsg<>nil then
  1010. internalerror(201001221);
  1011. if token=_CSTRING then
  1012. deprecatedmsg:=stringdup(cstringpattern)
  1013. else
  1014. deprecatedmsg:=stringdup(pattern);
  1015. consume(token);
  1016. include(moduleopt,mo_has_deprecated_msg);
  1017. end;
  1018. until false;
  1019. end;
  1020. {$ifdef jvm}
  1021. procedure addmoduleclass(curr : tmodule);
  1022. var
  1023. def: tobjectdef;
  1024. typesym: ttypesym;
  1025. begin
  1026. { java_jlobject may not have been parsed yet (system unit); in any
  1027. case, we only use this to refer to the class type, so inheritance
  1028. does not matter }
  1029. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  1030. include(def.objectoptions,oo_is_external);
  1031. include(def.objectoptions,oo_is_sealed);
  1032. def.objextname:=stringdup(curr.realmodulename^);
  1033. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  1034. symtablestack.top.insertsym(typesym);
  1035. end;
  1036. {$endif jvm}
  1037. type
  1038. tfinishstate=record
  1039. init_procinfo:tcgprocinfo;
  1040. finalize_procinfo:tcgprocinfo;
  1041. end;
  1042. pfinishstate=^tfinishstate;
  1043. function proc_unit_implementation(curr: tmodule):boolean;
  1044. var
  1045. init_procinfo,
  1046. finalize_procinfo : tcgprocinfo;
  1047. i,j : integer;
  1048. finishstate:pfinishstate;
  1049. begin
  1050. result:=true;
  1051. init_procinfo:=nil;
  1052. finalize_procinfo:=nil;
  1053. finishstate:=nil;
  1054. set_current_module(curr);
  1055. { We get here only after used modules were loaded }
  1056. connect_loaded_units(curr,curr.globalsymtable);
  1057. { All units are read, now give them a number }
  1058. curr.updatemaps;
  1059. { Consume the semicolon if needed.
  1060. At this point the units in the uses clause have at least been parsed
  1061. and are connected, and conditional compilation expressions can
  1062. use the symbols from those units }
  1063. if curr.consume_semicolon_after_uses then
  1064. begin
  1065. consume(_SEMICOLON);
  1066. curr.consume_semicolon_after_uses:=false;
  1067. end;
  1068. { further, changing the globalsymtable is not allowed anymore }
  1069. curr.globalsymtable.sealed:=true;
  1070. symtablestack.push(curr.localsymtable);
  1071. if not curr.interface_only then
  1072. begin
  1073. Message1(parser_u_parsing_implementation,curr.modulename^);
  1074. if curr.in_interface then
  1075. internalerror(200212285);
  1076. { Compile the unit }
  1077. init_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'init$'),potype_unitinit,curr.localsymtable);
  1078. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  1079. init_procinfo.parse_body;
  1080. { save file pos for debuginfo }
  1081. curr.mainfilepos:=init_procinfo.entrypos;
  1082. { parse finalization section }
  1083. if token=_FINALIZATION then
  1084. begin
  1085. { Compile the finalize }
  1086. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  1087. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  1088. finalize_procinfo.parse_body;
  1089. end
  1090. end;
  1091. { remove all units that we are waiting for that are already waiting for
  1092. us => breaking up circles }
  1093. for i:=0 to curr.waitingunits.count-1 do
  1094. for j:=curr.waitingforunit.count-1 downto 0 do
  1095. if curr.waitingunits[i]=curr.waitingforunit[j] then
  1096. curr.waitingforunit.Delete(j);
  1097. {$ifdef DEBUG_UNITWAITING}
  1098. Writeln('Unit ', curr.modulename^, ' is waiting for units: ');
  1099. for i:=curr.waitingforunit.count-1 downto 0 do
  1100. writeln(' ',i,'/',curr.waitingforunit.count,' ',tmodule(curr.waitingforunit[i]).realmodulename^);
  1101. {$endif}
  1102. result:=curr.waitingforunit.count=0;
  1103. { save all information that is needed for finishing the unit }
  1104. New(finishstate);
  1105. finishstate^.init_procinfo:=init_procinfo;
  1106. finishstate^.finalize_procinfo:=finalize_procinfo;
  1107. curr.finishstate:=finishstate;
  1108. if result then
  1109. result:=finish_compile_unit(curr)
  1110. else
  1111. curr.state:=ms_compiling_waitfinish;
  1112. end;
  1113. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  1114. begin
  1115. result:=true;
  1116. set_current_module(curr);
  1117. { update the symtable }
  1118. connect_loaded_units(curr,nil);
  1119. { We must do this again, because units can have been added to the list while another task was being handled }
  1120. curr.updatemaps;
  1121. { consume the semicolon after maps have been updated else conditional compiling expressions
  1122. might cause internal errors, see tw8611 }
  1123. if curr.consume_semicolon_after_uses then
  1124. begin
  1125. consume(_SEMICOLON);
  1126. curr.consume_semicolon_after_uses:=false;
  1127. end;
  1128. { now push our own symtable }
  1129. symtablestack.push(curr.globalsymtable);
  1130. { Dump stack
  1131. Write(curr.modulename^);
  1132. symtablestack.dump;
  1133. }
  1134. { ... parse the declarations }
  1135. Message1(parser_u_parsing_interface,curr.realmodulename^);
  1136. {$ifdef jvm}
  1137. { fake classdef to represent the class corresponding to the unit }
  1138. addmoduleclass(curr);
  1139. {$endif}
  1140. read_interface_declarations;
  1141. { Export macros defined in the interface for macpas. The macros
  1142. are put in the globalmacrosymtable that will only be used by other
  1143. units. The current unit continues to use the localmacrosymtable }
  1144. if (m_mac in current_settings.modeswitches) then
  1145. begin
  1146. curr.globalmacrosymtable:=tmacrosymtable.create(true);
  1147. curr.localmacrosymtable.SymList.ForEachCall(@copy_macro,curr);
  1148. end;
  1149. { leave when we got an error }
  1150. if (Errorcount>0) and not status.skip_error then
  1151. begin
  1152. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1153. status.skip_error:=true;
  1154. symtablestack.pop(curr.globalsymtable);
  1155. {$ifdef DEBUG_NODE_XML}
  1156. XMLFinalizeNodeFile('unit');
  1157. {$endif DEBUG_NODE_XML}
  1158. exit;
  1159. end;
  1160. { we need to be able to reference these in descendants,
  1161. so they must be generated and included in the interface }
  1162. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1163. add_synthetic_interface_classes_for_st(curr.globalsymtable,true,false);
  1164. { Our interface is compiled, generate interface CRC and switch to implementation }
  1165. {$IFDEF Debug_WaitCRC}
  1166. writeln('parse_unit_interface_declarations ',curr.realmodulename^);
  1167. {$ENDIF}
  1168. if not(cs_compilesystem in current_settings.moduleswitches) and
  1169. (Errorcount=0) then
  1170. tppumodule(curr).getppucrc;
  1171. curr.in_interface:=false;
  1172. curr.interface_compiled:=true;
  1173. { First reload all units depending on our interface, we need to do this
  1174. in the implementation part to prevent erroneous circular references }
  1175. tppumodule(curr).setdefgeneration;
  1176. tppumodule(curr).reload_flagged_units;
  1177. { Parse the implementation section }
  1178. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1179. curr.interface_only:=true
  1180. else
  1181. curr.interface_only:=false;
  1182. parse_only:=false;
  1183. { create static symbol table }
  1184. curr.localsymtable:=tstaticsymtable.create(curr.realmodulename^,curr.moduleid);
  1185. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1186. maybe_load_got(curr);
  1187. if not curr.interface_only then
  1188. begin
  1189. consume(_IMPLEMENTATION);
  1190. Message1(unit_u_loading_implementation_units,curr.modulename^);
  1191. { Read the implementation units }
  1192. if token=_USES then
  1193. begin
  1194. parseusesclause(curr);
  1195. if not loadunits(curr,false) then
  1196. curr.state:=ms_compiling_waitimpl;
  1197. { do not consume the semicolon yet, because the units in the uses clause
  1198. may not yet be loaded and conditional compilation expressions may
  1199. depend on symbols from those units }
  1200. curr.consume_semicolon_after_uses:=True;
  1201. end
  1202. else
  1203. curr.consume_semicolon_after_uses:=False;
  1204. end;
  1205. if curr.state in [ms_compiled,ms_processed] then
  1206. begin
  1207. // Writeln('Popping global symtable ?');
  1208. symtablestack.pop(curr.globalsymtable);
  1209. end;
  1210. { Can we continue compiling ? }
  1211. result:=curr.state<>ms_compiling_waitimpl;
  1212. if result then
  1213. result:=proc_unit_implementation(curr);
  1214. end;
  1215. function proc_unit(curr: tmodule):boolean;
  1216. var
  1217. main_file: tinputfile;
  1218. s1,s2 : ^string; {Saves stack space}
  1219. unitname : ansistring;
  1220. unitname8 : string[8];
  1221. feature : tfeature;
  1222. load_ok : boolean;
  1223. begin
  1224. result:=true;
  1225. if m_mac in current_settings.modeswitches then
  1226. curr.mode_switch_allowed:= false;
  1227. consume(_UNIT);
  1228. if curr.is_initial then
  1229. Status.IsExe:=false;
  1230. unitname:=orgpattern;
  1231. consume(_ID);
  1232. while token=_POINT do
  1233. begin
  1234. consume(_POINT);
  1235. unitname:=unitname+'.'+orgpattern;
  1236. consume(_ID);
  1237. end;
  1238. { create filenames and unit name }
  1239. main_file := current_scanner.inputfile;
  1240. while assigned(main_file.next) do
  1241. main_file := main_file.next;
  1242. new(s1);
  1243. s1^:=curr.modulename^;
  1244. curr.SetFileName(main_file.path+main_file.name,true);
  1245. curr.SetModuleName(unitname);
  1246. {$ifdef DEBUG_NODE_XML}
  1247. XMLInitializeNodeFile('unit', unitname);
  1248. {$endif DEBUG_NODE_XML}
  1249. { check for system unit }
  1250. new(s2);
  1251. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  1252. unitname8:=copy(curr.modulename^,1,8);
  1253. if (cs_check_unit_name in current_settings.globalswitches) and
  1254. (
  1255. not(
  1256. (curr.modulename^=s2^) or
  1257. (
  1258. (length(curr.modulename^)>8) and
  1259. (unitname8=s2^)
  1260. )
  1261. )
  1262. or
  1263. (
  1264. (length(s1^)>8) and
  1265. (s1^<>curr.modulename^)
  1266. )
  1267. ) then
  1268. Message2(unit_e_illegal_unit_name,curr.realmodulename^,s1^);
  1269. if (curr.modulename^='SYSTEM') then
  1270. include(current_settings.moduleswitches,cs_compilesystem);
  1271. dispose(s2);
  1272. dispose(s1);
  1273. if (target_info.system in systems_unit_program_exports) then
  1274. exportlib.preparelib(curr.realmodulename^);
  1275. { parse hint directives }
  1276. try_consume_hintdirective(curr.moduleoptions, curr.deprecatedmsg);
  1277. consume(_SEMICOLON);
  1278. { handle the global switches, do this before interface, because after interface has been
  1279. read, all following directives are parsed as well }
  1280. setupglobalswitches;
  1281. { generate now the global symboltable,
  1282. define first as local to overcome dependency conflicts }
  1283. curr.localsymtable:=tglobalsymtable.create(curr.modulename^,curr.moduleid);
  1284. { insert unitsym of this unit to prevent other units having
  1285. the same name }
  1286. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1287. { load default system unit, it must be loaded before interface is parsed
  1288. else we cannot use e.g. feature switches before the next real token }
  1289. load_ok:=loadsystemunit(curr);
  1290. { system unit is loaded, now insert feature defines }
  1291. for feature:=low(tfeature) to high(tfeature) do
  1292. if feature in features then
  1293. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1294. consume(_INTERFACE);
  1295. { global switches are read, so further changes aren't allowed }
  1296. curr.in_global:=false;
  1297. message1(unit_u_loading_interface_units,curr.modulename^);
  1298. { update status }
  1299. status.currentmodule:=curr.realmodulename^;
  1300. { maybe turn off m_objpas if we are compiling objpas }
  1301. if (curr.modulename^='OBJPAS') then
  1302. exclude(current_settings.modeswitches,m_objpas);
  1303. { maybe turn off m_mac if we are compiling macpas }
  1304. if (curr.modulename^='MACPAS') then
  1305. exclude(current_settings.modeswitches,m_mac);
  1306. parse_only:=true;
  1307. { load default units, like language mode units }
  1308. if not(cs_compilesystem in current_settings.moduleswitches) then
  1309. load_ok:=loaddefaultunits(curr) and load_ok;
  1310. { insert qualifier for the system unit (allows system.writeln) }
  1311. if not(cs_compilesystem in current_settings.moduleswitches) and
  1312. (token=_USES) then
  1313. begin
  1314. // We do this as late as possible.
  1315. if Assigned(curr) then
  1316. curr.Loadlocalnamespacelist
  1317. else
  1318. current_namespacelist:=Nil;
  1319. parseusesclause(curr);
  1320. load_ok:=loadunits(curr,true) and load_ok;
  1321. { has it been compiled at a higher level ?}
  1322. if curr.state in [ms_compiled,ms_processed] then
  1323. begin
  1324. Message1(parser_u_already_compiled,curr.realmodulename^);
  1325. exit;
  1326. end;
  1327. curr.consume_semicolon_after_uses:=true;
  1328. end
  1329. else
  1330. curr.consume_semicolon_after_uses:=false;
  1331. { move the global symtable from the temporary local to global }
  1332. current_module.globalsymtable:=current_module.localsymtable;
  1333. current_module.localsymtable:=nil;
  1334. { Now we check if we can continue. }
  1335. if not load_ok then
  1336. curr.state:=ms_compiling_waitintf;
  1337. { create whole program optimisation information (may already be
  1338. updated in the interface, e.g., in case of classrefdef typed
  1339. constants }
  1340. curr.wpoinfo:=tunitwpoinfo.create;
  1341. { Can we continue compiling ? }
  1342. result:=curr.state<>ms_compiling_waitintf;
  1343. if result then
  1344. result:=parse_unit_interface_declarations(curr);
  1345. end;
  1346. procedure module_is_done(curr: tmodule);inline;
  1347. begin
  1348. dispose(pfinishstate(curr.finishstate));
  1349. curr.finishstate:=nil;
  1350. end;
  1351. function finish_compile_unit(module: tmodule): boolean;
  1352. function is_assembler_generated:boolean;
  1353. var
  1354. hal : tasmlisttype;
  1355. begin
  1356. result:=false;
  1357. if Errorcount=0 then
  1358. begin
  1359. for hal:=low(TasmlistType) to high(TasmlistType) do
  1360. if not current_asmdata.asmlists[hal].empty then
  1361. begin
  1362. result:=true;
  1363. exit;
  1364. end;
  1365. end;
  1366. end;
  1367. var
  1368. {$ifdef EXTDEBUG}
  1369. store_crc,
  1370. {$endif EXTDEBUG}
  1371. force_init_final : boolean;
  1372. init_procinfo,
  1373. finalize_procinfo : tcgprocinfo;
  1374. i : longint;
  1375. ag : boolean;
  1376. finishstate : tfinishstate;
  1377. waitingmodule : tmodule;
  1378. begin
  1379. result:=true;
  1380. { curr is now module }
  1381. if not assigned(module.finishstate) then
  1382. internalerror(2012091801);
  1383. finishstate:=pfinishstate(module.finishstate)^;
  1384. finalize_procinfo:=finishstate.finalize_procinfo;
  1385. init_procinfo:=finishstate.init_procinfo;
  1386. { Generate specializations of objectdefs methods }
  1387. generate_specialization_procs;
  1388. // This needs to be done before we generate the VMTs
  1389. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1390. begin
  1391. add_synthetic_interface_classes_for_st(module.globalsymtable,false,true);
  1392. add_synthetic_interface_classes_for_st(module.localsymtable,true,true);
  1393. end;
  1394. { generate construction functions for all attributes in the unit:
  1395. this must be done before writing the VMTs because
  1396. during VMT writing the extended field info is written }
  1397. generate_attr_constrs(current_module.used_rtti_attrs);
  1398. { Generate VMTs }
  1399. if Errorcount=0 then
  1400. begin
  1401. write_vmts(module.globalsymtable,true);
  1402. write_vmts(module.localsymtable,false);
  1403. end;
  1404. { add implementations for synthetic method declarations added by
  1405. the compiler }
  1406. add_synthetic_method_implementations(module.globalsymtable);
  1407. add_synthetic_method_implementations(module.localsymtable);
  1408. { if the unit contains ansi/widestrings, initialization and
  1409. finalization code must be forced }
  1410. force_init_final:=tglobalsymtable(module.globalsymtable).needs_init_final or
  1411. tstaticsymtable(module.localsymtable).needs_init_final;
  1412. { should we force unit initialization? }
  1413. { this is a hack, but how can it be done better ? }
  1414. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1415. is it needed at all? (Sergei) }
  1416. { it's needed in case cnodeutils.force_init = true }
  1417. if (force_init_final or cnodeutils.force_init) and
  1418. (
  1419. not assigned(init_procinfo) or
  1420. has_no_code(init_procinfo.code)
  1421. ) then
  1422. begin
  1423. { first release the not used init procinfo }
  1424. if assigned(init_procinfo) then
  1425. begin
  1426. release_proc_symbol(init_procinfo.procdef);
  1427. release_main_proc(module,init_procinfo);
  1428. end;
  1429. init_procinfo:=gen_implicit_initfinal(module,mf_init,module.localsymtable);
  1430. end;
  1431. if (force_init_final or cnodeutils.force_final) and
  1432. (
  1433. not assigned(finalize_procinfo) or
  1434. has_no_code(finalize_procinfo.code)
  1435. ) then
  1436. begin
  1437. { first release the not used finalize procinfo }
  1438. if assigned(finalize_procinfo) then
  1439. begin
  1440. release_proc_symbol(finalize_procinfo.procdef);
  1441. release_main_proc(module,finalize_procinfo);
  1442. end;
  1443. finalize_procinfo:=gen_implicit_initfinal(module,mf_finalize,module.localsymtable);
  1444. end;
  1445. { Now both init and finalize bodies are read and it is known
  1446. which variables are used in both init and finalize we can now
  1447. generate the code. This is required to prevent putting a variable in
  1448. a register that is also used in the finalize body (PFV) }
  1449. if assigned(init_procinfo) then
  1450. begin
  1451. if (force_init_final or cnodeutils.force_init) or
  1452. not(has_no_code(init_procinfo.code)) then
  1453. begin
  1454. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1455. init_procinfo.generate_code_tree;
  1456. include(module.moduleflags,mf_init);
  1457. end
  1458. else
  1459. release_proc_symbol(init_procinfo.procdef);
  1460. init_procinfo.resetprocdef;
  1461. release_main_proc(module,init_procinfo);
  1462. end;
  1463. if assigned(finalize_procinfo) then
  1464. begin
  1465. if force_init_final or
  1466. cnodeutils.force_init or
  1467. not(has_no_code(finalize_procinfo.code)) then
  1468. begin
  1469. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1470. finalize_procinfo.generate_code_tree;
  1471. include(module.moduleflags,mf_finalize);
  1472. end
  1473. else
  1474. release_proc_symbol(finalize_procinfo.procdef);
  1475. finalize_procinfo.resetprocdef;
  1476. release_main_proc(module,finalize_procinfo);
  1477. end;
  1478. symtablestack.pop(module.localsymtable);
  1479. symtablestack.pop(module.globalsymtable);
  1480. { the last char should always be a point }
  1481. { Do not attempt to read next token after dot,
  1482. there may be a #0 when the unit was finished in a separate stage }
  1483. consume_last_dot;
  1484. { reset wpo flags for all defs }
  1485. reset_all_defs(module);
  1486. if (Errorcount=0) then
  1487. begin
  1488. { tests, if all (interface) forwards are resolved }
  1489. tstoredsymtable(module.globalsymtable).check_forwards;
  1490. { check if all private fields are used }
  1491. tstoredsymtable(module.globalsymtable).allprivatesused;
  1492. { test static symtable }
  1493. tstoredsymtable(module.localsymtable).allsymbolsused;
  1494. tstoredsymtable(module.localsymtable).allprivatesused;
  1495. tstoredsymtable(module.localsymtable).check_forwards;
  1496. tstoredsymtable(module.localsymtable).checklabels;
  1497. { used units }
  1498. module.allunitsused;
  1499. end;
  1500. { leave when we got an error }
  1501. if (Errorcount>0) and not status.skip_error then
  1502. begin
  1503. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1504. status.skip_error:=true;
  1505. module_is_done(module);
  1506. {$ifdef DEBUG_NODE_XML}
  1507. XMLFinalizeNodeFile('unit');
  1508. {$endif DEBUG_NODE_XML}
  1509. exit;
  1510. end;
  1511. { if an Objective-C module, generate rtti and module info }
  1512. MaybeGenerateObjectiveCImageInfo(module.globalsymtable,module.localsymtable);
  1513. { do we need to add the variants unit? }
  1514. maybeloadvariantsunit(module);
  1515. { generate rtti/init tables }
  1516. write_persistent_type_info(module.globalsymtable,true);
  1517. write_persistent_type_info(module.localsymtable,false);
  1518. { Tables }
  1519. cnodeutils.InsertThreadvars;
  1520. { Resource strings }
  1521. GenerateResourceStrings;
  1522. { Widestring typed constants }
  1523. cnodeutils.InsertWideInits;
  1524. { Resourcestring references }
  1525. cnodeutils.InsertResStrInits;
  1526. { generate debuginfo }
  1527. if (cs_debuginfo in current_settings.moduleswitches) then
  1528. current_debuginfo.inserttypeinfo;
  1529. { generate imports }
  1530. if module.ImportLibraryList.Count>0 then
  1531. importlib.generatelib;
  1532. { insert own objectfile, or say that it's in a library
  1533. (no check for an .o when loading) }
  1534. ag:=is_assembler_generated;
  1535. if ag then
  1536. insertobjectfile(module)
  1537. else
  1538. begin
  1539. module.headerflags:=module.headerflags or uf_no_link;
  1540. exclude(module.moduleflags,mf_has_stabs_debuginfo);
  1541. exclude(module.moduleflags,mf_has_dwarf_debuginfo);
  1542. end;
  1543. if ag then
  1544. begin
  1545. { create callframe info }
  1546. create_dwarf_frame;
  1547. { assemble }
  1548. create_objectfile(module);
  1549. end;
  1550. // remove all waits for this unit
  1551. for i:=0 to module.waitingunits.count-1 do
  1552. begin
  1553. waitingmodule:=tmodule(module.waitingunits[i]);
  1554. waitingmodule.remove_from_waitingforunits(module);
  1555. end;
  1556. // compute CRC
  1557. if ErrorCount=0 then
  1558. begin
  1559. if not module.usedunitsfinalcrc(waitingmodule) then
  1560. begin
  1561. { Some used units are still compiling, so their CRCs can change.
  1562. Compute the final CRC of this module, for the case of a
  1563. circular dependency, and wait.
  1564. }
  1565. {$IFDEF Debug_WaitCRC}
  1566. writeln('finish_compile_unit ',module.realmodulename^,' waiting for used unit CRCs...');
  1567. {$ENDIF}
  1568. tppumodule(module).getppucrc;
  1569. module.crc_final:=true;
  1570. module.state:=ms_compiled_waitcrc;
  1571. exit(false);
  1572. end;
  1573. end;
  1574. result:=finish_unit(module);
  1575. end;
  1576. function finish_unit(module: tmodule): boolean;
  1577. var
  1578. {$ifdef EXTDEBUG}
  1579. store_crc,
  1580. {$endif EXTDEBUG}
  1581. store_interface_crc,
  1582. store_indirect_crc : cardinal;
  1583. i : longint;
  1584. waitingmodule : tmodule;
  1585. begin
  1586. result:=true;
  1587. { Write out the ppufile after the object file has been created }
  1588. store_interface_crc:=module.interface_crc;
  1589. store_indirect_crc:=module.indirect_crc;
  1590. {$ifdef EXTDEBUG}
  1591. store_crc:=module.crc;
  1592. {$endif EXTDEBUG}
  1593. if ErrorCount=0 then
  1594. tppumodule(module).writeppu;
  1595. if not(cs_compilesystem in current_settings.moduleswitches) then
  1596. begin
  1597. if store_interface_crc<>module.interface_crc then
  1598. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1599. if store_indirect_crc<>module.indirect_crc then
  1600. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1601. end;
  1602. {$ifdef EXTDEBUG}
  1603. if not(cs_compilesystem in current_settings.moduleswitches) then
  1604. if (store_crc<>module.crc) then
  1605. Message1(unit_u_implementation_crc_changed,module.ppufilename);
  1606. {$endif EXTDEBUG}
  1607. { release unregistered defs/syms from the localsymtable }
  1608. free_unregistered_localsymtable_elements(module);
  1609. { release local symtables that are not needed anymore }
  1610. free_localsymtables(module.globalsymtable);
  1611. free_localsymtables(module.localsymtable);
  1612. { leave when we got an error }
  1613. if (Errorcount>0) and not status.skip_error then
  1614. begin
  1615. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1616. status.skip_error:=true;
  1617. module_is_done(module);
  1618. {$ifdef DEBUG_NODE_XML}
  1619. XMLFinalizeNodeFile('unit');
  1620. {$endif DEBUG_NODE_XML}
  1621. exit;
  1622. end;
  1623. {$ifdef debug_devirt}
  1624. { print out all instantiated class/object types }
  1625. writeln('constructed object/class/classreftypes in ',module.realmodulename^);
  1626. for i := 0 to module.wpoinfo.createdobjtypes.count-1 do
  1627. begin
  1628. write(' ',tdef(module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1629. case tdef(module.wpoinfo.createdobjtypes[i]).typ of
  1630. objectdef:
  1631. case tobjectdef(module.wpoinfo.createdobjtypes[i]).objecttype of
  1632. odt_object:
  1633. writeln(' (object)');
  1634. odt_class:
  1635. writeln(' (class)');
  1636. else
  1637. internalerror(2008101103);
  1638. end;
  1639. else
  1640. internalerror(2008101104);
  1641. end;
  1642. end;
  1643. for i := 0 to module.wpoinfo.createdclassrefobjtypes.count-1 do
  1644. begin
  1645. write(' Class Of ',tdef(module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1646. case tdef(module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1647. objectdef:
  1648. case tobjectdef(module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1649. odt_class:
  1650. writeln(' (classrefdef)');
  1651. else
  1652. internalerror(2008101105);
  1653. end
  1654. else
  1655. internalerror(2008101102);
  1656. end;
  1657. end;
  1658. {$endif debug_devirt}
  1659. Message1(unit_u_finished_compiling,module.modulename^);
  1660. module_is_done(module);
  1661. module.end_of_parsing;
  1662. {$ifdef DEBUG_NODE_XML}
  1663. XMLFinalizeNodeFile('unit');
  1664. {$endif DEBUG_NODE_XML}
  1665. end;
  1666. function proc_package(curr: tmodule) : boolean;
  1667. var
  1668. main_file : tinputfile;
  1669. hp,hp2 : tmodule;
  1670. pkg : tpcppackage;
  1671. main_procinfo : tcgprocinfo;
  1672. force_init_final : boolean;
  1673. uu : tused_unit;
  1674. module_name: ansistring;
  1675. pentry: ppackageentry;
  1676. feature : tfeature;
  1677. begin
  1678. Result:=True;
  1679. Status.IsPackage:=true;
  1680. Status.IsExe:=true;
  1681. parse_only:=false;
  1682. main_procinfo:=nil;
  1683. {init_procinfo:=nil;
  1684. finalize_procinfo:=nil;}
  1685. if not (tf_supports_packages in target_info.flags) then
  1686. message1(parser_e_packages_not_supported,target_info.name);
  1687. if not RelocSectionSetExplicitly then
  1688. RelocSection:=true;
  1689. { Relocation works only without stabs under Windows when }
  1690. { external linker (LD) is used. LD generates relocs for }
  1691. { stab sections which is not loaded in memory. It causes }
  1692. { AV error when DLL is loaded and relocation is needed. }
  1693. { Internal linker does not have this problem. }
  1694. if RelocSection and
  1695. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1696. (cs_link_extern in current_settings.globalswitches) then
  1697. begin
  1698. include(current_settings.globalswitches,cs_link_strip);
  1699. { Warning stabs info does not work with reloc section !! }
  1700. if (cs_debuginfo in current_settings.moduleswitches) and
  1701. (target_dbg.id=dbg_stabs) then
  1702. begin
  1703. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1704. Message(parser_w_parser_win32_debug_needs_WN);
  1705. exclude(current_settings.moduleswitches,cs_debuginfo);
  1706. end;
  1707. end;
  1708. { get correct output names }
  1709. main_file := current_scanner.inputfile;
  1710. while assigned(main_file.next) do
  1711. main_file := main_file.next;
  1712. curr.SetFileName(main_file.path+main_file.name,true);
  1713. { consume _PACKAGE word }
  1714. consume(_ID);
  1715. module_name:=orgpattern;
  1716. consume(_ID);
  1717. while token=_POINT do
  1718. begin
  1719. consume(_POINT);
  1720. module_name:=module_name+'.'+orgpattern;
  1721. consume(_ID);
  1722. end;
  1723. curr.setmodulename(module_name);
  1724. curr.ispackage:=true;
  1725. exportlib.preparelib(module_name);
  1726. pkg:=tpcppackage.create(module_name);
  1727. if tf_library_needs_pic in target_info.flags then
  1728. include(current_settings.moduleswitches,cs_create_pic);
  1729. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1730. read, all following directives are parsed as well }
  1731. setupglobalswitches;
  1732. {$ifdef DEBUG_NODE_XML}
  1733. XMLInitializeNodeFile('package', module_name);
  1734. {$endif DEBUG_NODE_XML}
  1735. consume(_SEMICOLON);
  1736. { global switches are read, so further changes aren't allowed }
  1737. curr.in_global:=false;
  1738. { set implementation flag }
  1739. curr.in_interface:=false;
  1740. curr.interface_compiled:=true;
  1741. { insert after the unit symbol tables the static symbol table }
  1742. { of the program }
  1743. curr.localsymtable:=tstaticsymtable.create(curr.realmodulename^,curr.moduleid);
  1744. { ensure that no packages are picked up from the options }
  1745. packagelist.clear;
  1746. // There should always be a requires, except for the system package. So we load here
  1747. if Assigned(curr) then
  1748. curr.Loadlocalnamespacelist
  1749. else
  1750. current_namespacelist:=Nil;
  1751. {Read the packages used by the package we compile.}
  1752. if (token=_ID) and (idtoken=_REQUIRES) then
  1753. begin
  1754. { consume _REQUIRES word }
  1755. consume(_ID);
  1756. while true do
  1757. begin
  1758. if token=_ID then
  1759. begin
  1760. module_name:=orgpattern;
  1761. consume(_ID);
  1762. while token=_POINT do
  1763. begin
  1764. consume(_POINT);
  1765. module_name:=module_name+'.'+orgpattern;
  1766. consume(_ID);
  1767. end;
  1768. add_package(module_name,false,true);
  1769. end
  1770. else
  1771. consume(_ID);
  1772. if token=_COMMA then
  1773. consume(_COMMA)
  1774. else
  1775. break;
  1776. end;
  1777. consume(_SEMICOLON);
  1778. end;
  1779. { now load all packages, so that we can determine whether a unit is
  1780. already provided by one of the loaded packages }
  1781. load_packages;
  1782. if packagelist.Count>0 then
  1783. begin
  1784. { this means the SYSTEM unit *must* be part of one of the required
  1785. packages, so load it }
  1786. AddUnit(curr,'system',false);
  1787. systemunit:=tglobalsymtable(symtablestack.top);
  1788. load_intern_types;
  1789. { system unit is loaded, now insert feature defines }
  1790. for feature:=low(tfeature) to high(tfeature) do
  1791. if feature in features then
  1792. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1793. end;
  1794. {Load the units used by the program we compile.}
  1795. if (token=_ID) and (idtoken=_CONTAINS) then
  1796. begin
  1797. { consume _CONTAINS word }
  1798. consume(_ID);
  1799. while true do
  1800. begin
  1801. if token=_ID then
  1802. begin
  1803. module_name:=orgpattern;
  1804. consume(_ID);
  1805. while token=_POINT do
  1806. begin
  1807. consume(_POINT);
  1808. module_name:=module_name+'.'+orgpattern;
  1809. consume(_ID);
  1810. end;
  1811. hp:=AddUnit(curr,module_name);
  1812. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1813. begin
  1814. systemunit:=tglobalsymtable(hp.globalsymtable);
  1815. load_intern_types;
  1816. end;
  1817. end
  1818. else
  1819. consume(_ID);
  1820. if token=_COMMA then
  1821. consume(_COMMA)
  1822. else break;
  1823. end;
  1824. consume(_SEMICOLON);
  1825. end;
  1826. { All units are read, now give them a number }
  1827. curr.updatemaps;
  1828. hp:=tmodule(loaded_units.first);
  1829. while assigned(hp) do
  1830. begin
  1831. if (hp<>curr) and not assigned(hp.package) then
  1832. begin
  1833. if mf_package_deny in hp.moduleflags then
  1834. message1(package_e_unit_deny_package,hp.realmodulename^);
  1835. { part of the package's used, aka contained units? }
  1836. uu:=tused_unit(curr.used_units.first);
  1837. while assigned(uu) do
  1838. begin
  1839. if uu.u=hp then
  1840. break;
  1841. uu:=tused_unit(uu.next);
  1842. end;
  1843. if not assigned(uu) then
  1844. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1845. end;
  1846. { was this unit listed as a contained unit? If so => error }
  1847. if (hp<>curr) and assigned(hp.package) then
  1848. begin
  1849. uu:=tused_unit(curr.used_units.first);
  1850. while assigned(uu) do
  1851. begin
  1852. if uu.u=hp then
  1853. break;
  1854. uu:=tused_unit(uu.next);
  1855. end;
  1856. if assigned(uu) then
  1857. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1858. end;
  1859. hp:=tmodule(hp.next);
  1860. end;
  1861. {Insert the name of the main program into the symbol table.}
  1862. if curr.realmodulename^<>'' then
  1863. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1864. Message1(parser_u_parsing_implementation,curr.mainsource);
  1865. symtablestack.push(curr.localsymtable);
  1866. { create whole program optimisation information }
  1867. curr.wpoinfo:=tunitwpoinfo.create;
  1868. { should we force unit initialization? }
  1869. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1870. if force_init_final or cnodeutils.force_init then
  1871. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1872. { Add symbol to the exports section for win32 so smartlinking a
  1873. DLL will include the edata section }
  1874. if assigned(exportlib) and
  1875. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1876. (mf_has_exports in curr.moduleflags) then
  1877. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1878. { all labels must be defined before generating code }
  1879. if Errorcount=0 then
  1880. tstoredsymtable(curr.localsymtable).checklabels;
  1881. symtablestack.pop(curr.localsymtable);
  1882. { consume the last point }
  1883. consume(_END);
  1884. consume(_POINT);
  1885. if (Errorcount=0) then
  1886. begin
  1887. { test static symtable }
  1888. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1889. tstoredsymtable(curr.localsymtable).allprivatesused;
  1890. tstoredsymtable(curr.localsymtable).check_forwards;
  1891. { Note: all contained units are considered as used }
  1892. end;
  1893. if target_info.system in systems_all_windows+systems_nativent then
  1894. begin
  1895. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1896. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1897. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1898. main_procinfo.generate_code;
  1899. end;
  1900. {$ifdef DEBUG_NODE_XML}
  1901. XMLFinalizeNodeFile('package');
  1902. {$endif DEBUG_NODE_XML}
  1903. { leave when we got an error }
  1904. if (Errorcount>0) and not status.skip_error then
  1905. begin
  1906. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1907. status.skip_error:=true;
  1908. pkg.free;
  1909. pkg := nil;
  1910. exit;
  1911. end;
  1912. { remove all unused units, this happends when units are removed
  1913. from the uses clause in the source and the ppu was already being loaded }
  1914. hp:=tmodule(loaded_units.first);
  1915. while assigned(hp) do
  1916. begin
  1917. hp2:=hp;
  1918. hp:=tmodule(hp.next);
  1919. if assigned(hp2.package) then
  1920. add_package_unit_ref(hp2.package);
  1921. if hp2.is_unit and
  1922. not assigned(hp2.globalsymtable) then
  1923. loaded_units.remove(hp2);
  1924. end;
  1925. exportlib.ignoreduplicates:=true;
  1926. { force exports }
  1927. uu:=tused_unit(usedunits.first);
  1928. while assigned(uu) do
  1929. begin
  1930. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1931. begin
  1932. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1933. load_intern_types;
  1934. end;
  1935. if not assigned(uu.u.package) then
  1936. export_unit(uu.u);
  1937. uu:=tused_unit(uu.next);
  1938. end;
  1939. {$ifdef arm}
  1940. { Insert .pdata section for arm-wince.
  1941. It is needed for exception handling. }
  1942. if target_info.system in [system_arm_wince] then
  1943. InsertPData;
  1944. {$endif arm}
  1945. { generate debuginfo }
  1946. if (cs_debuginfo in current_settings.moduleswitches) then
  1947. current_debuginfo.inserttypeinfo;
  1948. exportlib.generatelib;
  1949. exportlib.ignoreduplicates:=false;
  1950. { create import libraries for all packages }
  1951. if packagelist.count>0 then
  1952. createimportlibfromexternals;
  1953. { generate imports }
  1954. if curr.ImportLibraryList.Count>0 then
  1955. importlib.generatelib;
  1956. { Reference all DEBUGINFO sections from the main .fpc section }
  1957. if (cs_debuginfo in current_settings.moduleswitches) then
  1958. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1959. { insert own objectfile }
  1960. insertobjectfile(curr);
  1961. { assemble and link }
  1962. create_objectfile(curr);
  1963. { We might need the symbols info if not using
  1964. the default do_extractsymbolinfo
  1965. which is a dummy function PM }
  1966. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1967. { release all local symtables that are not needed anymore }
  1968. if (not needsymbolinfo) then
  1969. free_localsymtables(curr.localsymtable);
  1970. { leave when we got an error }
  1971. if (Errorcount>0) and not status.skip_error then
  1972. begin
  1973. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1974. status.skip_error:=true;
  1975. pkg.free;
  1976. pkg := nil;
  1977. exit;
  1978. end;
  1979. if (not curr.is_unit) then
  1980. begin
  1981. { we add all loaded units that are not part of a package to the
  1982. package; this includes units in the "contains" section as well
  1983. as implicitely imported ones }
  1984. hp:=tmodule(loaded_units.first);
  1985. while assigned(hp) do
  1986. begin
  1987. if (hp<>curr) then
  1988. begin
  1989. if not assigned(hp.package) then
  1990. begin
  1991. pkg.addunit(hp);
  1992. check_for_indirect_package_usages(hp.used_units);
  1993. end
  1994. else
  1995. begin
  1996. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1997. if not assigned(pentry) then
  1998. internalerror(2015112301);
  1999. pkg.add_required_package(hp.package);
  2000. end;
  2001. end;
  2002. hp:=tmodule(hp.next);
  2003. end;
  2004. pkg.initmoduleinfo(curr);
  2005. { create the executable when we are at level 1 }
  2006. if (curr.is_initial) then
  2007. begin
  2008. { create global resource file by collecting all resource files }
  2009. CollectResourceFiles;
  2010. { write .def file }
  2011. if (cs_link_deffile in current_settings.globalswitches) then
  2012. deffile.writefile;
  2013. { generate the pcp file }
  2014. pkg.savepcp;
  2015. { insert all .o files from all loaded units and
  2016. unload the units, we don't need them anymore.
  2017. Keep the curr because that is still needed }
  2018. hp:=tmodule(loaded_units.first);
  2019. while assigned(hp) do
  2020. begin
  2021. { only link in those units which should become part of this
  2022. package }
  2023. if not assigned(hp.package) then
  2024. linker.AddModuleFiles(hp);
  2025. hp2:=tmodule(hp.next);
  2026. if (hp<>curr) and
  2027. (not needsymbolinfo) then
  2028. begin
  2029. loaded_units.remove(hp);
  2030. hp.free;
  2031. hp := nil;
  2032. end;
  2033. hp:=hp2;
  2034. end;
  2035. { add the library of directly used packages }
  2036. add_package_libs(linker);
  2037. { and now link the package library }
  2038. linker.MakeSharedLibrary
  2039. end;
  2040. { Give Fatal with error count for linker errors }
  2041. if (Errorcount>0) and not status.skip_error then
  2042. begin
  2043. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2044. status.skip_error:=true;
  2045. end;
  2046. pkg.free;
  2047. pkg := nil;
  2048. end;
  2049. end;
  2050. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  2051. var
  2052. program_uses_checkpointer : boolean;
  2053. hp,hp2 : tmodule;
  2054. begin
  2055. { create global resource file by collecting all resource files }
  2056. CollectResourceFiles;
  2057. { write .def file }
  2058. if (cs_link_deffile in current_settings.globalswitches) then
  2059. deffile.writefile;
  2060. { link SysInit (if any) first, to have behavior consistent with
  2061. assembler startup files }
  2062. if assigned(sysinitmod) then
  2063. linker.AddModuleFiles(sysinitmod);
  2064. { Does any unit use checkpointer function }
  2065. program_uses_checkpointer:=false;
  2066. { insert all .o files from all loaded units and
  2067. unload the units, we don't need them anymore.
  2068. Keep the curr because that is still needed }
  2069. hp:=tmodule(loaded_units.first);
  2070. while assigned(hp) do
  2071. begin
  2072. if (hp<>sysinitmod) and not assigned(hp.package) then
  2073. begin
  2074. linker.AddModuleFiles(hp);
  2075. if mf_checkpointer_called in hp.moduleflags then
  2076. program_uses_checkpointer:=true;
  2077. end;
  2078. hp2:=tmodule(hp.next);
  2079. if assigned(hp.package) then
  2080. add_package_unit_ref(hp.package);
  2081. if (hp<>curr) and
  2082. (not needsymbolinfo) then
  2083. begin
  2084. loaded_units.remove(hp);
  2085. hp.free;
  2086. hp := nil;
  2087. end;
  2088. hp:=hp2;
  2089. end;
  2090. { free also unneeded units we didn't free before }
  2091. if not needsymbolinfo then
  2092. unloaded_units.Clear;
  2093. { Does any unit use checkpointer function }
  2094. if program_uses_checkpointer then
  2095. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  2096. { add all directly used packages as libraries }
  2097. add_package_libs(linker);
  2098. { finally we can create an executable }
  2099. if curr.islibrary then
  2100. linker.MakeSharedLibrary
  2101. else
  2102. linker.MakeExecutable;
  2103. { collect all necessary information for whole-program optimization }
  2104. wpoinfomanager.extractwpoinfofromprogram;
  2105. end;
  2106. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  2107. var
  2108. sysinitmod, hp,hp2 : tmodule;
  2109. resources_used : boolean;
  2110. begin
  2111. sysinitmod:=nil;
  2112. hp:=nil;
  2113. hp2:=nil;
  2114. resources_used:=false;
  2115. {$ifdef DEBUG_NODE_XML}
  2116. if IsLibrary then
  2117. XMLFinalizeNodeFile('library')
  2118. else
  2119. XMLFinalizeNodeFile('program');
  2120. {$endif DEBUG_NODE_XML}
  2121. { reset wpo flags for all defs }
  2122. reset_all_defs(curr);
  2123. if (Errorcount=0) then
  2124. begin
  2125. { test static symtable }
  2126. tstoredsymtable(curr.localsymtable).allsymbolsused;
  2127. tstoredsymtable(curr.localsymtable).allprivatesused;
  2128. tstoredsymtable(curr.localsymtable).check_forwards;
  2129. curr.allunitsused;
  2130. end;
  2131. { leave when we got an error }
  2132. if (Errorcount>0) and not status.skip_error then
  2133. begin
  2134. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2135. status.skip_error:=true;
  2136. exit;
  2137. end;
  2138. { remove all unused units, this happens when units are removed
  2139. from the uses clause in the source and the ppu was already being loaded }
  2140. hp:=tmodule(loaded_units.first);
  2141. while assigned(hp) do
  2142. begin
  2143. hp2:=hp;
  2144. hp:=tmodule(hp.next);
  2145. if hp2.is_unit and
  2146. not assigned(hp2.globalsymtable) then
  2147. begin
  2148. loaded_units.remove(hp2);
  2149. unloaded_units.concat(hp2);
  2150. end;
  2151. end;
  2152. { do we need to add the variants unit? }
  2153. maybeloadvariantsunit(curr);
  2154. { Now that everything has been compiled we know if we need resource
  2155. support. If not, remove the unit. }
  2156. resources_used:=MaybeRemoveResUnit(curr);
  2157. linker.initsysinitunitname;
  2158. if target_info.system in systems_internal_sysinit then
  2159. begin
  2160. { add start/halt unit }
  2161. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  2162. end
  2163. else
  2164. sysinitmod:=nil;
  2165. {$ifdef arm}
  2166. { Insert .pdata section for arm-wince.
  2167. It is needed for exception handling. }
  2168. if target_info.system in [system_arm_wince] then
  2169. InsertPData;
  2170. {$endif arm}
  2171. cnodeutils.InsertThreadvars;
  2172. { generate rtti/init tables }
  2173. write_persistent_type_info(curr.localsymtable,false);
  2174. { if an Objective-C module, generate rtti and module info }
  2175. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  2176. { generate debuginfo }
  2177. if (cs_debuginfo in current_settings.moduleswitches) then
  2178. current_debuginfo.inserttypeinfo;
  2179. if islibrary or (target_info.system in systems_unit_program_exports) then
  2180. exportlib.generatelib;
  2181. { Reference all DEBUGINFO sections from the main .fpc section }
  2182. if (cs_debuginfo in current_settings.moduleswitches) then
  2183. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2184. { Resource strings }
  2185. GenerateResourceStrings;
  2186. { Windows widestring needing initialization }
  2187. cnodeutils.InsertWideInits;
  2188. { Resourcestring references (const foo:string=someresourcestring) }
  2189. cnodeutils.InsertResStrInits;
  2190. { insert Tables and StackLength }
  2191. cnodeutils.InsertInitFinalTable(curr);
  2192. cnodeutils.InsertThreadvarTablesTable;
  2193. cnodeutils.InsertResourceTablesTable;
  2194. cnodeutils.InsertWideInitsTablesTable;
  2195. cnodeutils.InsertResStrTablesTable;
  2196. cnodeutils.InsertMemorySizes;
  2197. { Insert symbol to resource info }
  2198. cnodeutils.InsertResourceInfo(resources_used);
  2199. { create callframe info }
  2200. create_dwarf_frame;
  2201. { create import library for all packages }
  2202. if packagelist.count>0 then
  2203. createimportlibfromexternals;
  2204. { generate imports }
  2205. if curr.ImportLibraryList.Count>0 then
  2206. importlib.generatelib;
  2207. { insert own objectfile }
  2208. insertobjectfile(curr);
  2209. { assemble and link }
  2210. create_objectfile(curr);
  2211. { We might need the symbols info if not using
  2212. the default do_extractsymbolinfo
  2213. which is a dummy function PM }
  2214. needsymbolinfo:=
  2215. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2216. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2217. { release all local symtables that are not needed anymore }
  2218. if (not needsymbolinfo) then
  2219. free_localsymtables(curr.localsymtable);
  2220. { leave when we got an error }
  2221. if (Errorcount>0) and not status.skip_error then
  2222. begin
  2223. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2224. status.skip_error:=true;
  2225. exit;
  2226. end;
  2227. { create the executable when we are at level 1 }
  2228. if (not curr.is_unit) and (curr.is_initial) then
  2229. proc_create_executable(curr,sysinitmod,islibrary);
  2230. { Give Fatal with error count for linker errors }
  2231. if (Errorcount>0) and not status.skip_error then
  2232. begin
  2233. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2234. status.skip_error:=true;
  2235. end;
  2236. curr.state:=ms_processed;
  2237. end;
  2238. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  2239. var
  2240. initpd : tprocdef;
  2241. finalize_procinfo,
  2242. init_procinfo,
  2243. main_procinfo : tcgprocinfo;
  2244. force_init_final : boolean;
  2245. begin
  2246. result:=true;
  2247. main_procinfo:=nil;
  2248. init_procinfo:=nil;
  2249. finalize_procinfo:=nil;
  2250. set_current_module(curr);
  2251. { All units are read, now give them a number }
  2252. curr.updatemaps;
  2253. connect_loaded_units(curr,nil);
  2254. { consume the semicolon after maps have been updated else conditional compiling expressions
  2255. might cause internal errors, see tw8611 }
  2256. if curr.consume_semicolon_after_uses then
  2257. begin
  2258. consume(_SEMICOLON);
  2259. curr.consume_semicolon_after_uses:=false;
  2260. end;
  2261. {Insert the name of the main program into the symbol table.}
  2262. if curr.realmodulename^<>'' then
  2263. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  2264. Message1(parser_u_parsing_implementation,curr.mainsource);
  2265. symtablestack.push(curr.localsymtable);
  2266. {$ifdef jvm}
  2267. { fake classdef to represent the class corresponding to the unit }
  2268. addmoduleclass(curr);
  2269. {$endif}
  2270. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  2271. maybe_load_got(curr);
  2272. { create whole program optimisation information }
  2273. curr.wpoinfo:=tunitwpoinfo.create;
  2274. { The program intialization needs an alias, so it can be called
  2275. from the bootstrap code.}
  2276. if islibrary then
  2277. begin
  2278. initpd:=nil;
  2279. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2280. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2281. if target_info.system in systems_darwin then
  2282. begin
  2283. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2284. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2285. initpd:=main_procinfo.procdef;
  2286. main_procinfo.free;
  2287. main_procinfo := nil;
  2288. end;
  2289. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2290. { Win32 startup code needs a single name }
  2291. if not(target_info.system in (systems_darwin+systems_aix)) then
  2292. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2293. else
  2294. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2295. if not(target_info.system in systems_darwin) then
  2296. initpd:=main_procinfo.procdef;
  2297. cnodeutils.RegisterModuleInitFunction(initpd);
  2298. end
  2299. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2300. begin
  2301. { create a stub with the name of the desired main routine, with
  2302. the same signature as the C "main" function, and call through to
  2303. FPC_SYSTEMMAIN, which will initialise everything based on its
  2304. parameters. This function cannot be in the system unit, because
  2305. its name can be configured on the command line (for use with e.g.
  2306. SDL, where the main function should be called SDL_main) }
  2307. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2308. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2309. main_procinfo.free;
  2310. { now create the PASCALMAIN routine (which will be called from
  2311. FPC_SYSTEMMAIN) }
  2312. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2313. end
  2314. else
  2315. begin
  2316. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2317. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2318. end;
  2319. main_procinfo.parse_body;
  2320. { save file pos for debuginfo }
  2321. curr.mainfilepos:=main_procinfo.entrypos;
  2322. { finalize? }
  2323. if token=_FINALIZATION then
  2324. begin
  2325. { Parse the finalize }
  2326. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2327. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2328. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2329. finalize_procinfo.parse_body;
  2330. end;
  2331. { Generate specializations of objectdefs methods }
  2332. if Errorcount=0 then
  2333. generate_specialization_procs;
  2334. { This needs to be done before we generate the VMTs }
  2335. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2336. add_synthetic_interface_classes_for_st(curr.localsymtable,true,true);
  2337. { generate construction functions for all attributes in the program }
  2338. { before write_vmts that asume attributes for methods is ready }
  2339. generate_attr_constrs(curr.used_rtti_attrs);
  2340. { Generate VMTs }
  2341. if Errorcount=0 then
  2342. write_vmts(curr.localsymtable,false);
  2343. { add implementations for synthetic method declarations added by
  2344. the compiler }
  2345. add_synthetic_method_implementations(curr.localsymtable);
  2346. { should we force unit initialization? }
  2347. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2348. if force_init_final or cnodeutils.force_init then
  2349. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2350. { Add symbol to the exports section for win32 so smartlinking a
  2351. DLL will include the edata section }
  2352. if assigned(exportlib) and
  2353. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2354. (mf_has_exports in curr.moduleflags) then
  2355. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2356. if (force_init_final or cnodeutils.force_final) and
  2357. (
  2358. not assigned(finalize_procinfo)
  2359. or has_no_code(finalize_procinfo.code)
  2360. ) then
  2361. begin
  2362. { first release the not used finalize procinfo }
  2363. if assigned(finalize_procinfo) then
  2364. begin
  2365. release_proc_symbol(finalize_procinfo.procdef);
  2366. release_main_proc(curr,finalize_procinfo);
  2367. end;
  2368. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2369. end;
  2370. { the finalization routine of libraries is generic (and all libraries need to }
  2371. { be finalized, so they can finalize any units they use }
  2372. { Place in "pure assembler" list so that the llvm assembler writer
  2373. directly emits the generated directives }
  2374. if (islibrary) then
  2375. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2376. { all labels must be defined before generating code }
  2377. if Errorcount=0 then
  2378. tstoredsymtable(curr.localsymtable).checklabels;
  2379. { See remark in unit init/final }
  2380. main_procinfo.generate_code_tree;
  2381. main_procinfo.resetprocdef;
  2382. release_main_proc(curr,main_procinfo);
  2383. if assigned(init_procinfo) then
  2384. begin
  2385. { initialization can be implicit only }
  2386. include(curr.moduleflags,mf_init);
  2387. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2388. init_procinfo.generate_code;
  2389. init_procinfo.resetprocdef;
  2390. release_main_proc(curr,init_procinfo);
  2391. end;
  2392. if assigned(finalize_procinfo) then
  2393. begin
  2394. if force_init_final or
  2395. cnodeutils.force_init or
  2396. not(has_no_code(finalize_procinfo.code)) then
  2397. begin
  2398. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2399. finalize_procinfo.generate_code_tree;
  2400. include(curr.moduleflags,mf_finalize);
  2401. end;
  2402. finalize_procinfo.resetprocdef;
  2403. release_main_proc(curr,finalize_procinfo);
  2404. end;
  2405. symtablestack.pop(curr.localsymtable);
  2406. { consume the last point }
  2407. consume(_POINT);
  2408. proc_program_after_parsing(curr,islibrary);
  2409. end;
  2410. procedure proc_library_header(curr: tmodule);
  2411. var
  2412. program_name : ansistring;
  2413. begin
  2414. consume(_LIBRARY);
  2415. program_name:=orgpattern;
  2416. consume(_ID);
  2417. while token=_POINT do
  2418. begin
  2419. consume(_POINT);
  2420. program_name:=program_name+'.'+orgpattern;
  2421. consume(_ID);
  2422. end;
  2423. curr.setmodulename(program_name);
  2424. curr.islibrary:=true;
  2425. exportlib.preparelib(program_name);
  2426. if tf_library_needs_pic in target_info.flags then
  2427. begin
  2428. include(current_settings.moduleswitches,cs_create_pic);
  2429. { also set create_pic for all unit compilation }
  2430. include(init_settings.moduleswitches,cs_create_pic);
  2431. end;
  2432. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2433. read, all following directives are parsed as well }
  2434. setupglobalswitches;
  2435. {$ifdef DEBUG_NODE_XML}
  2436. XMLInitializeNodeFile('library', program_name);
  2437. {$endif DEBUG_NODE_XML}
  2438. end;
  2439. type
  2440. TProgramParam = record
  2441. name : ansistring;
  2442. nr : dword;
  2443. end;
  2444. TProgramParamArray = array of TProgramParam;
  2445. procedure proc_program_header(curr: tmodule; out sc : TProgramParamArray);
  2446. var
  2447. program_name : ansistring;
  2448. paramnum : integer;
  2449. begin
  2450. sc:=nil;
  2451. consume(_PROGRAM);
  2452. program_name:=orgpattern;
  2453. consume(_ID);
  2454. while token=_POINT do
  2455. begin
  2456. consume(_POINT);
  2457. program_name:=program_name+'.'+orgpattern;
  2458. consume(_ID);
  2459. end;
  2460. curr.setmodulename(program_name);
  2461. if (target_info.system in systems_unit_program_exports) then
  2462. exportlib.preparelib(program_name);
  2463. if token=_LKLAMMER then
  2464. begin
  2465. consume(_LKLAMMER);
  2466. paramnum:=1;
  2467. repeat
  2468. if m_isolike_program_para in current_settings.modeswitches then
  2469. begin
  2470. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2471. begin
  2472. { the symtablestack is not setup here, so text must be created later on }
  2473. Setlength(sc,length(sc)+1);
  2474. with sc[high(sc)] do
  2475. begin
  2476. name:=pattern;
  2477. nr:=paramnum;
  2478. end;
  2479. inc(paramnum);
  2480. end;
  2481. end;
  2482. consume(_ID);
  2483. until not try_to_consume(_COMMA);
  2484. consume(_RKLAMMER);
  2485. end;
  2486. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2487. read, all following directives are parsed as well }
  2488. setupglobalswitches;
  2489. {$ifdef DEBUG_NODE_XML}
  2490. XMLInitializeNodeFile('program', program_name);
  2491. {$endif DEBUG_NODE_XML}
  2492. end;
  2493. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  2494. var
  2495. main_file : tinputfile;
  2496. consume_semicolon_after_loaded : boolean;
  2497. ps : tprogramparasym;
  2498. textsym : ttypesym;
  2499. sc : TProgramParamArray;
  2500. i : Longint;
  2501. feature : tfeature;
  2502. load_ok : boolean;
  2503. begin
  2504. result:=true;
  2505. Status.IsLibrary:=IsLibrary;
  2506. Status.IsPackage:=false;
  2507. Status.IsExe:=true;
  2508. parse_only:=false;
  2509. consume_semicolon_after_loaded:=false;
  2510. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2511. sc:=nil;
  2512. { DLL defaults to create reloc info }
  2513. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2514. begin
  2515. if not RelocSectionSetExplicitly then
  2516. RelocSection:=true;
  2517. end;
  2518. { Relocation works only without stabs under Windows when }
  2519. { external linker (LD) is used. LD generates relocs for }
  2520. { stab sections which is not loaded in memory. It causes }
  2521. { AV error when DLL is loaded and relocation is needed. }
  2522. { Internal linker does not have this problem. }
  2523. if RelocSection and
  2524. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2525. (cs_link_extern in current_settings.globalswitches) then
  2526. begin
  2527. include(current_settings.globalswitches,cs_link_strip);
  2528. { Warning stabs info does not work with reloc section !! }
  2529. if (cs_debuginfo in current_settings.moduleswitches) and
  2530. (target_dbg.id=dbg_stabs) then
  2531. begin
  2532. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2533. Message(parser_w_parser_win32_debug_needs_WN);
  2534. exclude(current_settings.moduleswitches,cs_debuginfo);
  2535. end;
  2536. end;
  2537. { get correct output names }
  2538. main_file := current_scanner.inputfile;
  2539. while assigned(main_file.next) do
  2540. main_file := main_file.next;
  2541. curr.SetFileName(main_file.path+main_file.name,true);
  2542. if islibrary then
  2543. begin
  2544. proc_library_header(curr);
  2545. consume_semicolon_after_loaded:=true;
  2546. end
  2547. else if token=_PROGRAM then
  2548. { is there an program head ? }
  2549. begin
  2550. proc_program_header(curr,sc);
  2551. consume_semicolon_after_loaded:=true;
  2552. end
  2553. else
  2554. begin
  2555. if (target_info.system in systems_unit_program_exports) then
  2556. exportlib.preparelib(curr.realmodulename^);
  2557. { setup things using the switches }
  2558. setupglobalswitches;
  2559. {$ifdef DEBUG_NODE_XML}
  2560. XMLInitializeNodeFile('program', curr.realmodulename^);
  2561. {$endif DEBUG_NODE_XML}
  2562. end;
  2563. { load all packages, so we know whether a unit is contained inside a
  2564. package or not }
  2565. load_packages;
  2566. { set implementation flag }
  2567. curr.in_interface:=false;
  2568. curr.interface_compiled:=true;
  2569. { insert after the unit symbol tables the static symbol table
  2570. of the program }
  2571. curr.localsymtable:=tstaticsymtable.create(curr.realmodulename^,curr.moduleid);
  2572. { load system unit }
  2573. load_ok:=loadsystemunit(curr);
  2574. { consume the semicolon now that the system unit is loaded }
  2575. if consume_semicolon_after_loaded then
  2576. consume(_SEMICOLON);
  2577. { global switches are read, so further changes aren't allowed }
  2578. curr.in_global:=false;
  2579. { system unit is loaded, now insert feature defines }
  2580. for feature:=low(tfeature) to high(tfeature) do
  2581. if feature in features then
  2582. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2583. { load standard units, e.g objpas,profile unit }
  2584. load_ok:=loaddefaultunits(curr) and load_ok;
  2585. { Load units provided on the command line }
  2586. load_ok:=loadautounits(curr) and load_ok;
  2587. { insert iso program parameters }
  2588. if length(sc)>0 then
  2589. begin
  2590. textsym:=search_system_type('TEXT');
  2591. if not(assigned(textsym)) then
  2592. internalerror(2013011201);
  2593. for i:=0 to high(sc) do
  2594. begin
  2595. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2596. curr.localsymtable.insertsym(ps,true);
  2597. end;
  2598. end;
  2599. { Load the units used by the program we compile. }
  2600. if token=_USES then
  2601. begin
  2602. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2603. if Assigned(curr) then
  2604. curr.Loadlocalnamespacelist
  2605. else
  2606. current_namespacelist:=Nil;
  2607. parseusesclause(curr);
  2608. load_ok:=loadunits(curr,false) and load_ok;
  2609. curr.consume_semicolon_after_uses:=true;
  2610. end
  2611. else
  2612. curr.consume_semicolon_after_uses:=false;
  2613. if not load_ok then
  2614. curr.state:=ms_compiling_wait;
  2615. { Can we continue compiling ? }
  2616. result:=curr.state<>ms_compiling_wait;
  2617. if result then
  2618. result:=proc_program_declarations(curr,islibrary)
  2619. end;
  2620. end.