symsym.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. Implementation for the symbols types of the symtable
  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 symsym;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. { target }
  24. globtype,globals,widestr,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,defcmp,
  27. { ppu }
  28. ppu,finput,
  29. cclasses,symnot,
  30. { aasm }
  31. aasmbase,
  32. cpuinfo,cpubase,cgbase,cgutils,parabase
  33. ;
  34. type
  35. { this class is the base for all symbol objects }
  36. tstoredsym = class(tsym)
  37. public
  38. constructor create(st:tsymtyp;const n : string);
  39. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  40. destructor destroy;override;
  41. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  42. end;
  43. tlabelsym = class(tstoredsym)
  44. used,
  45. defined,
  46. nonlocal : boolean;
  47. { points to the matching node, only valid resultdef pass is run and
  48. the goto<->label relation in the node tree is created, should
  49. be a tnode }
  50. code : pointer;
  51. { when the label is defined in an asm block, this points to the
  52. generated asmlabel }
  53. asmblocklabel : tasmlabel;
  54. constructor create(const n : string);
  55. constructor ppuload(ppufile:tcompilerppufile);
  56. procedure ppuwrite(ppufile:tcompilerppufile);override;
  57. function mangledname:string;override;
  58. end;
  59. tunitsym = class(Tstoredsym)
  60. module : tobject; { tmodule }
  61. constructor create(const n : string;amodule : tobject);
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. destructor destroy;override;
  64. procedure ppuwrite(ppufile:tcompilerppufile);override;
  65. end;
  66. terrorsym = class(Tsym)
  67. constructor create;
  68. end;
  69. { tprocsym }
  70. tprocsym = class(tstoredsym)
  71. protected
  72. FProcdefList : TFPObjectList;
  73. FProcdefDerefList : TFPList;
  74. public
  75. constructor create(const n : string);
  76. constructor ppuload(ppufile:tcompilerppufile);
  77. destructor destroy;override;
  78. { writes all declarations except the specified one }
  79. procedure write_parameter_lists(skipdef:tprocdef);
  80. { tests, if all procedures definitions are defined and not }
  81. { only forward }
  82. procedure check_forward;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. procedure buildderef;override;
  85. procedure deref;override;
  86. function find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  87. function find_procdef_bypara(para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  88. function find_procdef_byoptions(ops:tprocoptions): Tprocdef;
  89. function find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  90. function find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  91. function find_procdef_enumerator_operator(typedef:tdef;var besteq:tequaltype):Tprocdef;
  92. property ProcdefList:TFPObjectList read FProcdefList;
  93. end;
  94. ttypesym = class(Tstoredsym)
  95. typedef : tdef;
  96. typedefderef : tderef;
  97. constructor create(const n : string;def:tdef);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. procedure ppuwrite(ppufile:tcompilerppufile);override;
  100. procedure buildderef;override;
  101. procedure deref;override;
  102. end;
  103. tabstractvarsym = class(tstoredsym)
  104. varoptions : tvaroptions;
  105. notifications : Tlinkedlist;
  106. varspez : tvarspez; { sets the type of access }
  107. varregable : tvarregable;
  108. varstate : tvarstate;
  109. { Has the address of this variable potentially escaped the }
  110. { block in which is was declared? }
  111. { could also be part of tabstractnormalvarsym, but there's }
  112. { one byte left here till the next 4 byte alignment }
  113. addr_taken : boolean;
  114. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  115. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  116. destructor destroy;override;
  117. procedure ppuwrite(ppufile:tcompilerppufile);override;
  118. procedure buildderef;override;
  119. procedure deref;override;
  120. function getsize : aint;
  121. function getpackedbitsize : longint;
  122. function is_regvar(refpara: boolean):boolean;
  123. procedure trigger_notifications(what:Tnotification_flag);
  124. function register_notification(flags:Tnotification_flags;
  125. callback:Tnotification_callback):cardinal;
  126. procedure unregister_notification(id:cardinal);
  127. private
  128. _vardef : tdef;
  129. vardefderef : tderef;
  130. procedure setvardef(def:tdef);
  131. public
  132. property vardef: tdef read _vardef write setvardef;
  133. end;
  134. tfieldvarsym = class(tabstractvarsym)
  135. fieldoffset : aint; { offset in record/object }
  136. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  137. constructor ppuload(ppufile:tcompilerppufile);
  138. procedure ppuwrite(ppufile:tcompilerppufile);override;
  139. function mangledname:string;override;
  140. end;
  141. tabstractnormalvarsym = class(tabstractvarsym)
  142. defaultconstsym : tsym;
  143. defaultconstsymderef : tderef;
  144. localloc : TLocation; { register/reference for local var }
  145. initialloc : TLocation; { initial location so it can still be initialized later after the location was changed by SSA }
  146. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  147. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  148. procedure ppuwrite(ppufile:tcompilerppufile);override;
  149. procedure buildderef;override;
  150. procedure deref;override;
  151. end;
  152. tlocalvarsym = class(tabstractnormalvarsym)
  153. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  154. constructor ppuload(ppufile:tcompilerppufile);
  155. procedure ppuwrite(ppufile:tcompilerppufile);override;
  156. end;
  157. tparavarsym = class(tabstractnormalvarsym)
  158. paraloc : array[tcallercallee] of TCGPara;
  159. paranr : word; { position of this parameter }
  160. {$ifdef EXTDEBUG}
  161. eqval : tequaltype;
  162. {$endif EXTDEBUG}
  163. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  164. constructor ppuload(ppufile:tcompilerppufile);
  165. destructor destroy;override;
  166. procedure ppuwrite(ppufile:tcompilerppufile);override;
  167. end;
  168. tstaticvarsym = class(tabstractnormalvarsym)
  169. private
  170. _mangledname : pshortstring;
  171. public
  172. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  173. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);
  174. constructor create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  175. constructor ppuload(ppufile:tcompilerppufile);
  176. destructor destroy;override;
  177. procedure ppuwrite(ppufile:tcompilerppufile);override;
  178. function mangledname:string;override;
  179. procedure set_mangledname(const s:string);
  180. end;
  181. tabsolutevarsym = class(tabstractvarsym)
  182. public
  183. abstyp : absolutetyp;
  184. {$ifdef i386}
  185. absseg : boolean;
  186. {$endif i386}
  187. asmname : pshortstring;
  188. addroffset : aword;
  189. ref : tpropaccesslist;
  190. constructor create(const n : string;def:tdef);
  191. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  192. destructor destroy;override;
  193. constructor ppuload(ppufile:tcompilerppufile);
  194. procedure buildderef;override;
  195. procedure deref;override;
  196. function mangledname : string;override;
  197. procedure ppuwrite(ppufile:tcompilerppufile);override;
  198. end;
  199. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  200. tpropertysym = class(Tstoredsym)
  201. propoptions : tpropertyoptions;
  202. overridenpropsym : tpropertysym;
  203. overridenpropsymderef : tderef;
  204. propdef : tdef;
  205. propdefderef : tderef;
  206. indexdef : tdef;
  207. indexdefderef : tderef;
  208. index,
  209. default : longint;
  210. propaccesslist : array[tpropaccesslisttypes] of tpropaccesslist;
  211. constructor create(const n : string);
  212. destructor destroy;override;
  213. constructor ppuload(ppufile:tcompilerppufile);
  214. function getsize : aint;
  215. procedure ppuwrite(ppufile:tcompilerppufile);override;
  216. procedure buildderef;override;
  217. procedure deref;override;
  218. end;
  219. tconstvalue = record
  220. case integer of
  221. 0: (valueord : tconstexprint);
  222. 1: (valueordptr : tconstptruint);
  223. 2: (valueptr : pointer; len : longint);
  224. end;
  225. tconstsym = class(tstoredsym)
  226. constdef : tdef;
  227. constdefderef : tderef;
  228. consttyp : tconsttyp;
  229. value : tconstvalue;
  230. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  231. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  232. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  233. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  234. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  235. constructor ppuload(ppufile:tcompilerppufile);
  236. destructor destroy;override;
  237. procedure buildderef;override;
  238. procedure deref;override;
  239. procedure ppuwrite(ppufile:tcompilerppufile);override;
  240. end;
  241. tenumsym = class(Tstoredsym)
  242. value : longint;
  243. definition : tenumdef;
  244. definitionderef : tderef;
  245. nextenum : tenumsym;
  246. constructor create(const n : string;def : tenumdef;v : longint);
  247. constructor ppuload(ppufile:tcompilerppufile);
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;
  249. procedure buildderef;override;
  250. procedure deref;override;
  251. procedure order;
  252. end;
  253. tsyssym = class(Tstoredsym)
  254. number : longint;
  255. constructor create(const n : string;l : longint);
  256. constructor ppuload(ppufile:tcompilerppufile);
  257. destructor destroy;override;
  258. procedure ppuwrite(ppufile:tcompilerppufile);override;
  259. end;
  260. const
  261. maxmacrolen=16*1024;
  262. type
  263. pmacrobuffer = ^tmacrobuffer;
  264. tmacrobuffer = array[0..maxmacrolen-1] of char;
  265. tmacro = class(tstoredsym)
  266. {Normally true, but false when a previously defined macro is undef-ed}
  267. defined : boolean;
  268. {True if this is a mac style compiler variable, in which case no macro
  269. substitutions shall be done.}
  270. is_compiler_var : boolean;
  271. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  272. {e. g. an IFDEF which returns false, will not be registered as used,}
  273. {since there is no place to register its use. }
  274. is_used : boolean;
  275. buftext : pchar;
  276. buflen : longint;
  277. constructor create(const n : string);
  278. constructor ppuload(ppufile:tcompilerppufile);
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;
  280. destructor destroy;override;
  281. function GetCopy:tmacro;
  282. end;
  283. var
  284. generrorsym : tsym;
  285. implementation
  286. uses
  287. { global }
  288. verbose,
  289. { target }
  290. systems,
  291. { symtable }
  292. defutil,symtable,
  293. fmodule,
  294. { tree }
  295. node,
  296. { aasm }
  297. aasmtai,aasmdata,
  298. { codegen }
  299. paramgr,
  300. procinfo
  301. ;
  302. {****************************************************************************
  303. Helpers
  304. ****************************************************************************}
  305. {****************************************************************************
  306. TSYM (base for all symtypes)
  307. ****************************************************************************}
  308. constructor tstoredsym.create(st:tsymtyp;const n : string);
  309. begin
  310. inherited create(st,n);
  311. { Register in current_module }
  312. if assigned(current_module) then
  313. begin
  314. current_module.symlist.Add(self);
  315. SymId:=current_module.symlist.Count-1;
  316. end;
  317. end;
  318. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  319. begin
  320. SymId:=ppufile.getlongint;
  321. inherited Create(st,ppufile.getstring);
  322. { Register symbol }
  323. current_module.symlist[SymId]:=self;
  324. ppufile.getposinfo(fileinfo);
  325. visibility:=tvisibility(ppufile.getbyte);
  326. ppufile.getsmallset(symoptions);
  327. if sp_has_deprecated_msg in symoptions then
  328. deprecatedmsg:=stringdup(ppufile.getstring)
  329. else
  330. deprecatedmsg:=nil;
  331. end;
  332. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  333. begin
  334. ppufile.putlongint(SymId);
  335. ppufile.putstring(realname);
  336. ppufile.putposinfo(fileinfo);
  337. ppufile.putbyte(byte(visibility));
  338. ppufile.putsmallset(symoptions);
  339. if sp_has_deprecated_msg in symoptions then
  340. ppufile.putstring(deprecatedmsg^);
  341. end;
  342. destructor tstoredsym.destroy;
  343. begin
  344. inherited destroy;
  345. end;
  346. {****************************************************************************
  347. TLABELSYM
  348. ****************************************************************************}
  349. constructor tlabelsym.create(const n : string);
  350. begin
  351. inherited create(labelsym,n);
  352. used:=false;
  353. defined:=false;
  354. nonlocal:=false;
  355. code:=nil;
  356. end;
  357. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  358. begin
  359. inherited ppuload(labelsym,ppufile);
  360. code:=nil;
  361. used:=false;
  362. nonlocal:=false;
  363. defined:=true;
  364. end;
  365. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  366. begin
  367. if owner.symtabletype=globalsymtable then
  368. Message(sym_e_ill_label_decl)
  369. else
  370. begin
  371. inherited ppuwrite(ppufile);
  372. ppufile.writeentry(iblabelsym);
  373. end;
  374. end;
  375. function tlabelsym.mangledname:string;
  376. begin
  377. if not(defined) then
  378. begin
  379. defined:=true;
  380. if nonlocal then
  381. current_asmdata.getglobaljumplabel(asmblocklabel)
  382. else
  383. current_asmdata.getjumplabel(asmblocklabel);
  384. end;
  385. result:=asmblocklabel.name;
  386. end;
  387. {****************************************************************************
  388. TUNITSYM
  389. ****************************************************************************}
  390. constructor tunitsym.create(const n : string;amodule : tobject);
  391. begin
  392. inherited create(unitsym,n);
  393. module:=amodule;
  394. end;
  395. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  396. begin
  397. inherited ppuload(unitsym,ppufile);
  398. module:=nil;
  399. end;
  400. destructor tunitsym.destroy;
  401. begin
  402. inherited destroy;
  403. end;
  404. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  405. begin
  406. inherited ppuwrite(ppufile);
  407. ppufile.writeentry(ibunitsym);
  408. end;
  409. {****************************************************************************
  410. TPROCSYM
  411. ****************************************************************************}
  412. constructor tprocsym.create(const n : string);
  413. begin
  414. inherited create(procsym,n);
  415. FProcdefList:=TFPObjectList.Create(false);
  416. FProcdefderefList:=nil;
  417. { the tprocdef have their own symoptions, make the procsym
  418. always visible }
  419. visibility:=vis_public;
  420. end;
  421. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  422. var
  423. pdderef : tderef;
  424. i,
  425. pdcnt : longint;
  426. begin
  427. inherited ppuload(procsym,ppufile);
  428. FProcdefList:=TFPObjectList.Create(false);
  429. FProcdefDerefList:=TFPList.Create;
  430. pdcnt:=ppufile.getword;
  431. for i:=1 to pdcnt do
  432. begin
  433. ppufile.getderef(pdderef);
  434. FProcdefDerefList.Add(Pointer(PtrInt(pdderef.dataidx)));
  435. end;
  436. end;
  437. destructor tprocsym.destroy;
  438. begin
  439. FProcdefList.Free;
  440. if assigned(FProcdefDerefList) then
  441. FProcdefDerefList.Free;
  442. inherited destroy;
  443. end;
  444. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  445. var
  446. i : longint;
  447. d : tderef;
  448. begin
  449. inherited ppuwrite(ppufile);
  450. ppufile.putword(FProcdefDerefList.Count);
  451. for i:=0 to FProcdefDerefList.Count-1 do
  452. begin
  453. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  454. ppufile.putderef(d);
  455. end;
  456. ppufile.writeentry(ibprocsym);
  457. end;
  458. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  459. var
  460. i : longint;
  461. pd : tprocdef;
  462. begin
  463. for i:=0 to ProcdefList.Count-1 do
  464. begin
  465. pd:=tprocdef(ProcdefList[i]);
  466. if pd<>skipdef then
  467. MessagePos1(pd.fileinfo,sym_h_param_list,pd.fullprocname(false));
  468. end;
  469. end;
  470. procedure tprocsym.check_forward;
  471. var
  472. i : longint;
  473. pd : tprocdef;
  474. begin
  475. for i:=0 to ProcdefList.Count-1 do
  476. begin
  477. pd:=tprocdef(ProcdefList[i]);
  478. if (pd.owner=owner) and (pd.forwarddef) then
  479. begin
  480. { For mode macpas. Make implicit externals (procedures declared in the interface
  481. section which do not have a counterpart in the implementation)
  482. to be an imported procedure }
  483. if (m_mac in current_settings.modeswitches) and
  484. (pd.interfacedef) then
  485. begin
  486. pd.setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  487. if (not current_module.interface_only) then
  488. MessagePos1(pd.fileinfo,sym_w_forward_not_resolved,pd.fullprocname(false));
  489. end
  490. else
  491. begin
  492. MessagePos1(pd.fileinfo,sym_e_forward_not_resolved,pd.fullprocname(false));
  493. end;
  494. { Turn further error messages off }
  495. pd.forwarddef:=false;
  496. end;
  497. end;
  498. end;
  499. procedure tprocsym.buildderef;
  500. var
  501. i : longint;
  502. pd : tprocdef;
  503. d : tderef;
  504. begin
  505. if not assigned(FProcdefDerefList) then
  506. FProcdefDerefList:=TFPList.Create
  507. else
  508. FProcdefDerefList.Clear;
  509. for i:=0 to ProcdefList.Count-1 do
  510. begin
  511. pd:=tprocdef(ProcdefList[i]);
  512. { only write the proc definitions that belong
  513. to this procsym and are in the global symtable }
  514. if pd.owner=owner then
  515. begin
  516. d.build(pd);
  517. FProcdefDerefList.Add(Pointer(PtrInt(d.dataidx)));
  518. end;
  519. end;
  520. end;
  521. procedure tprocsym.deref;
  522. var
  523. i : longint;
  524. pd : tprocdef;
  525. d : tderef;
  526. begin
  527. { Clear all procdefs }
  528. ProcdefList.Clear;
  529. if not assigned(FProcdefDerefList) then
  530. internalerror(200611031);
  531. for i:=0 to FProcdefDerefList.Count-1 do
  532. begin
  533. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  534. pd:=tprocdef(d.resolve);
  535. ProcdefList.Add(pd);
  536. end;
  537. end;
  538. function Tprocsym.Find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  539. var
  540. i : longint;
  541. pd : tprocdef;
  542. begin
  543. result:=nil;
  544. for i:=0 to ProcdefList.Count-1 do
  545. begin
  546. pd:=tprocdef(ProcdefList[i]);
  547. if pd.proctypeoption=pt then
  548. begin
  549. result:=pd;
  550. exit;
  551. end;
  552. end;
  553. end;
  554. function Tprocsym.Find_procdef_bypara(para:TFPObjectList;retdef:tdef;
  555. cpoptions:tcompare_paras_options):Tprocdef;
  556. var
  557. i : longint;
  558. pd : tprocdef;
  559. eq : tequaltype;
  560. begin
  561. result:=nil;
  562. for i:=0 to ProcdefList.Count-1 do
  563. begin
  564. pd:=tprocdef(ProcdefList[i]);
  565. if assigned(retdef) then
  566. eq:=compare_defs(retdef,pd.returndef,nothingn)
  567. else
  568. eq:=te_equal;
  569. if (eq>=te_equal) or
  570. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  571. begin
  572. eq:=compare_paras(para,pd.paras,cp_value_equal_const,cpoptions);
  573. if (eq>=te_equal) or
  574. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  575. begin
  576. result:=pd;
  577. exit;
  578. end;
  579. end;
  580. end;
  581. end;
  582. function tprocsym.find_procdef_byoptions(ops: tprocoptions): Tprocdef;
  583. var
  584. i : longint;
  585. pd : tprocdef;
  586. begin
  587. result:=nil;
  588. for i:=0 to ProcdefList.Count-1 do
  589. begin
  590. pd:=tprocdef(ProcdefList[i]);
  591. if ops * pd.procoptions = ops then
  592. begin
  593. result:=pd;
  594. exit;
  595. end;
  596. end;
  597. end;
  598. function Tprocsym.Find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  599. var
  600. i : longint;
  601. bestpd,
  602. pd : tprocdef;
  603. eq,besteq : tequaltype;
  604. begin
  605. { This function will return the pprocdef of pprocsym that
  606. is the best match for procvardef. When there are multiple
  607. matches it returns nil.}
  608. result:=nil;
  609. bestpd:=nil;
  610. besteq:=te_incompatible;
  611. for i:=0 to ProcdefList.Count-1 do
  612. begin
  613. pd:=tprocdef(ProcdefList[i]);
  614. eq:=proc_to_procvar_equal(pd,d);
  615. if eq>=te_equal then
  616. begin
  617. { multiple procvars with the same equal level }
  618. if assigned(bestpd) and
  619. (besteq=eq) then
  620. exit;
  621. if eq>besteq then
  622. begin
  623. besteq:=eq;
  624. bestpd:=pd;
  625. end;
  626. end;
  627. end;
  628. result:=bestpd;
  629. end;
  630. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  631. var
  632. paraidx, realparamcount,
  633. i, j : longint;
  634. bestpd,
  635. hpd,
  636. pd : tprocdef;
  637. convtyp : tconverttype;
  638. eq : tequaltype;
  639. begin
  640. { This function will return the pprocdef of pprocsym that
  641. is the best match for procvardef. When there are multiple
  642. matches it returns nil.}
  643. result:=nil;
  644. bestpd:=nil;
  645. besteq:=te_incompatible;
  646. for i:=0 to ProcdefList.Count-1 do
  647. begin
  648. pd:=tprocdef(ProcdefList[i]);
  649. if (equal_defs(todef,pd.returndef) or
  650. { shortstrings of different lengths are ok as result }
  651. (is_shortstring(todef) and is_shortstring(pd.returndef))) and
  652. { the result type must be always really equal and not an alias,
  653. if you mess with this code, check tw4093 }
  654. ((todef=pd.returndef) or
  655. (
  656. not(df_unique in todef.defoptions) and
  657. not(df_unique in pd.returndef.defoptions)
  658. )
  659. ) then
  660. begin
  661. paraidx:=0;
  662. { ignore vs_hidden parameters }
  663. while (paraidx<pd.paras.count) and
  664. assigned(pd.paras[paraidx]) and
  665. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  666. inc(paraidx);
  667. realparamcount:=0;
  668. for j := 0 to pd.paras.Count-1 do
  669. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  670. inc(realparamcount);
  671. if (paraidx<pd.paras.count) and
  672. assigned(pd.paras[paraidx]) and
  673. (realparamcount = 1) then
  674. begin
  675. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  676. { alias? if yes, only l1 choice,
  677. if you mess with this code, check tw4093 }
  678. if (eq=te_exact) and
  679. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  680. ((df_unique in fromdef.defoptions) or
  681. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  682. eq:=te_convert_l1;
  683. if eq=te_exact then
  684. begin
  685. besteq:=eq;
  686. result:=pd;
  687. exit;
  688. end;
  689. if eq>besteq then
  690. begin
  691. bestpd:=pd;
  692. besteq:=eq;
  693. end;
  694. end;
  695. end;
  696. end;
  697. result:=bestpd;
  698. end;
  699. function Tprocsym.find_procdef_enumerator_operator(typedef:tdef;var besteq:tequaltype):Tprocdef;
  700. var
  701. paraidx, realparamcount,
  702. i, j : longint;
  703. bestpd,
  704. hpd,
  705. pd : tprocdef;
  706. convtyp : tconverttype;
  707. eq : tequaltype;
  708. begin
  709. { This function will return the pprocdef of pprocsym that
  710. is the best match for procvardef. When there are multiple
  711. matches it returns nil.}
  712. result:=nil;
  713. bestpd:=nil;
  714. besteq:=te_incompatible;
  715. for i:=0 to ProcdefList.Count-1 do
  716. begin
  717. pd:=tprocdef(ProcdefList[i]);
  718. paraidx:=0;
  719. { ignore vs_hidden parameters }
  720. while (paraidx<pd.paras.count) and
  721. assigned(pd.paras[paraidx]) and
  722. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  723. inc(paraidx);
  724. realparamcount:=0;
  725. for j := 0 to pd.paras.Count-1 do
  726. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  727. inc(realparamcount);
  728. if (paraidx<pd.paras.count) and
  729. assigned(pd.paras[paraidx]) and
  730. (realparamcount = 1) then
  731. begin
  732. eq:=compare_defs_ext(typedef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  733. { alias? if yes, only l1 choice,
  734. if you mess with this code, check tw4093 }
  735. if (eq=te_exact) and
  736. (typedef<>tparavarsym(pd.paras[paraidx]).vardef) and
  737. ((df_unique in typedef.defoptions) or
  738. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  739. eq:=te_convert_l1;
  740. if eq=te_exact then
  741. begin
  742. besteq:=eq;
  743. result:=pd;
  744. exit;
  745. end;
  746. if eq>besteq then
  747. begin
  748. bestpd:=pd;
  749. besteq:=eq;
  750. end;
  751. end;
  752. end;
  753. result:=bestpd;
  754. end;
  755. {****************************************************************************
  756. TERRORSYM
  757. ****************************************************************************}
  758. constructor terrorsym.create;
  759. begin
  760. inherited create(errorsym,'');
  761. end;
  762. {****************************************************************************
  763. TPROPERTYSYM
  764. ****************************************************************************}
  765. constructor tpropertysym.create(const n : string);
  766. var
  767. pap : tpropaccesslisttypes;
  768. begin
  769. inherited create(propertysym,n);
  770. propoptions:=[];
  771. index:=0;
  772. default:=0;
  773. propdef:=nil;
  774. indexdef:=nil;
  775. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  776. propaccesslist[pap]:=tpropaccesslist.create;
  777. end;
  778. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  779. var
  780. pap : tpropaccesslisttypes;
  781. begin
  782. inherited ppuload(propertysym,ppufile);
  783. ppufile.getsmallset(propoptions);
  784. ppufile.getderef(overridenpropsymderef);
  785. ppufile.getderef(propdefderef);
  786. index:=ppufile.getlongint;
  787. default:=ppufile.getlongint;
  788. ppufile.getderef(indexdefderef);
  789. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  790. propaccesslist[pap]:=ppufile.getpropaccesslist;
  791. end;
  792. destructor tpropertysym.destroy;
  793. var
  794. pap : tpropaccesslisttypes;
  795. begin
  796. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  797. propaccesslist[pap].free;
  798. inherited destroy;
  799. end;
  800. procedure tpropertysym.buildderef;
  801. var
  802. pap : tpropaccesslisttypes;
  803. begin
  804. overridenpropsymderef.build(overridenpropsym);
  805. propdefderef.build(propdef);
  806. indexdefderef.build(indexdef);
  807. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  808. propaccesslist[pap].buildderef;
  809. end;
  810. procedure tpropertysym.deref;
  811. var
  812. pap : tpropaccesslisttypes;
  813. begin
  814. overridenpropsym:=tpropertysym(overridenpropsymderef.resolve);
  815. indexdef:=tdef(indexdefderef.resolve);
  816. propdef:=tdef(propdefderef.resolve);
  817. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  818. propaccesslist[pap].resolve;
  819. end;
  820. function tpropertysym.getsize : aint;
  821. begin
  822. getsize:=0;
  823. end;
  824. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  825. var
  826. pap : tpropaccesslisttypes;
  827. begin
  828. inherited ppuwrite(ppufile);
  829. ppufile.putsmallset(propoptions);
  830. ppufile.putderef(overridenpropsymderef);
  831. ppufile.putderef(propdefderef);
  832. ppufile.putlongint(index);
  833. ppufile.putlongint(default);
  834. ppufile.putderef(indexdefderef);
  835. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  836. ppufile.putpropaccesslist(propaccesslist[pap]);
  837. ppufile.writeentry(ibpropertysym);
  838. end;
  839. {****************************************************************************
  840. TABSTRACTVARSYM
  841. ****************************************************************************}
  842. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  843. begin
  844. inherited create(st,n);
  845. vardef:=def;
  846. varspez:=vsp;
  847. varstate:=vs_declared;
  848. varoptions:=vopts;
  849. end;
  850. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  851. begin
  852. inherited ppuload(st,ppufile);
  853. varstate:=vs_readwritten;
  854. varspez:=tvarspez(ppufile.getbyte);
  855. varregable:=tvarregable(ppufile.getbyte);
  856. addr_taken:=boolean(ppufile.getbyte);
  857. ppufile.getderef(vardefderef);
  858. ppufile.getsmallset(varoptions);
  859. end;
  860. destructor tabstractvarsym.destroy;
  861. begin
  862. if assigned(notifications) then
  863. notifications.destroy;
  864. inherited destroy;
  865. end;
  866. procedure tabstractvarsym.buildderef;
  867. begin
  868. vardefderef.build(vardef);
  869. end;
  870. procedure tabstractvarsym.deref;
  871. var
  872. oldvarregable: tvarregable;
  873. begin
  874. { setting the vardef also updates varregable. We just loaded this }
  875. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  876. { tw7817b.pp: the address is taken of a local variable in an }
  877. { inlined procedure -> must remain non-regable when inlining) }
  878. oldvarregable:=varregable;
  879. vardef:=tdef(vardefderef.resolve);
  880. varregable:=oldvarregable;
  881. end;
  882. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  883. var
  884. oldintfcrc : boolean;
  885. begin
  886. inherited ppuwrite(ppufile);
  887. ppufile.putbyte(byte(varspez));
  888. oldintfcrc:=ppufile.do_crc;
  889. ppufile.do_crc:=false;
  890. ppufile.putbyte(byte(varregable));
  891. ppufile.putbyte(byte(addr_taken));
  892. ppufile.do_crc:=oldintfcrc;
  893. ppufile.putderef(vardefderef);
  894. ppufile.putsmallset(varoptions);
  895. end;
  896. function tabstractvarsym.getsize : aint;
  897. begin
  898. if assigned(vardef) and
  899. ((vardef.typ<>arraydef) or
  900. is_dynamic_array(vardef) or
  901. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  902. result:=vardef.size
  903. else
  904. result:=0;
  905. end;
  906. function tabstractvarsym.getpackedbitsize : longint;
  907. begin
  908. { bitpacking is only done for ordinals }
  909. if not is_ordinal(vardef) then
  910. internalerror(2006082010);
  911. result:=vardef.packedbitsize;
  912. end;
  913. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  914. begin
  915. { Register variables are not allowed in the following cases:
  916. - regvars are disabled
  917. - exceptions are used (after an exception is raised the contents of the
  918. registers is not valid anymore)
  919. - it has a local copy
  920. - the value needs to be in memory (i.e. reference counted) }
  921. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  922. not(pi_has_assembler_block in current_procinfo.flags) and
  923. not(pi_uses_exceptions in current_procinfo.flags) and
  924. not(vo_has_local_copy in varoptions) and
  925. ((refpara and
  926. (varregable <> vr_none)) or
  927. (not refpara and
  928. not(varregable in [vr_none,vr_addr])))
  929. {$if not defined(powerpc) and not defined(powerpc64)}
  930. and ((vardef.typ <> recorddef) or
  931. (varregable = vr_addr) or
  932. not(varstate in [vs_written,vs_readwritten]));
  933. {$endif}
  934. end;
  935. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  936. var n:Tnotification;
  937. begin
  938. if assigned(notifications) then
  939. begin
  940. n:=Tnotification(notifications.first);
  941. while assigned(n) do
  942. begin
  943. if what in n.flags then
  944. n.callback(what,self);
  945. n:=Tnotification(n.next);
  946. end;
  947. end;
  948. end;
  949. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  950. Tnotification_callback):cardinal;
  951. var n:Tnotification;
  952. begin
  953. if not assigned(notifications) then
  954. notifications:=Tlinkedlist.create;
  955. n:=Tnotification.create(flags,callback);
  956. register_notification:=n.id;
  957. notifications.concat(n);
  958. end;
  959. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  960. var n:Tnotification;
  961. begin
  962. if not assigned(notifications) then
  963. internalerror(200212311)
  964. else
  965. begin
  966. n:=Tnotification(notifications.first);
  967. while assigned(n) do
  968. begin
  969. if n.id=id then
  970. begin
  971. notifications.remove(n);
  972. n.destroy;
  973. exit;
  974. end;
  975. n:=Tnotification(n.next);
  976. end;
  977. internalerror(200212311)
  978. end;
  979. end;
  980. procedure tabstractvarsym.setvardef(def:tdef);
  981. begin
  982. _vardef := def;
  983. { can we load the value into a register ? }
  984. if not assigned(owner) or
  985. (owner.symtabletype in [localsymtable,parasymtable]) or
  986. (
  987. (owner.symtabletype=staticsymtable) and
  988. not(cs_create_pic in current_settings.moduleswitches)
  989. ) then
  990. begin
  991. if tstoreddef(vardef).is_intregable then
  992. varregable:=vr_intreg
  993. else
  994. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  995. if {(
  996. not assigned(owner) or
  997. (owner.symtabletype<>staticsymtable)
  998. ) and }
  999. tstoreddef(vardef).is_fpuregable then
  1000. begin
  1001. {$ifdef x86}
  1002. if use_sse(vardef) then
  1003. varregable:=vr_mmreg
  1004. else
  1005. {$else x86}
  1006. varregable:=vr_fpureg;
  1007. {$endif x86}
  1008. end;
  1009. end;
  1010. end;
  1011. {****************************************************************************
  1012. TFIELDVARSYM
  1013. ****************************************************************************}
  1014. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1015. begin
  1016. inherited create(fieldvarsym,n,vsp,def,vopts);
  1017. fieldoffset:=-1;
  1018. end;
  1019. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1020. begin
  1021. inherited ppuload(fieldvarsym,ppufile);
  1022. fieldoffset:=ppufile.getaint;
  1023. end;
  1024. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1025. begin
  1026. inherited ppuwrite(ppufile);
  1027. ppufile.putaint(fieldoffset);
  1028. ppufile.writeentry(ibfieldvarsym);
  1029. end;
  1030. function tfieldvarsym.mangledname:string;
  1031. var
  1032. srsym : tsym;
  1033. srsymtable : tsymtable;
  1034. begin
  1035. if sp_static in symoptions then
  1036. begin
  1037. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1038. result:=srsym.mangledname
  1039. { when generating the debug info for the module in which the }
  1040. { symbol is defined, the localsymtable of that module is }
  1041. { already popped from the symtablestack }
  1042. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1043. result:=srsym.mangledname
  1044. else
  1045. internalerror(2007012501);
  1046. end
  1047. else
  1048. result:=inherited mangledname;
  1049. end;
  1050. {****************************************************************************
  1051. TABSTRACTNORMALVARSYM
  1052. ****************************************************************************}
  1053. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1054. begin
  1055. inherited create(st,n,vsp,def,vopts);
  1056. fillchar(localloc,sizeof(localloc),0);
  1057. fillchar(initialloc,sizeof(initialloc),0);
  1058. defaultconstsym:=nil;
  1059. end;
  1060. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1061. begin
  1062. inherited ppuload(st,ppufile);
  1063. fillchar(localloc,sizeof(localloc),0);
  1064. fillchar(initialloc,sizeof(initialloc),0);
  1065. ppufile.getderef(defaultconstsymderef);
  1066. end;
  1067. procedure tabstractnormalvarsym.buildderef;
  1068. begin
  1069. inherited buildderef;
  1070. defaultconstsymderef.build(defaultconstsym);
  1071. end;
  1072. procedure tabstractnormalvarsym.deref;
  1073. begin
  1074. inherited deref;
  1075. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1076. end;
  1077. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1078. begin
  1079. inherited ppuwrite(ppufile);
  1080. ppufile.putderef(defaultconstsymderef);
  1081. end;
  1082. {****************************************************************************
  1083. Tstaticvarsym
  1084. ****************************************************************************}
  1085. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1086. begin
  1087. inherited create(staticvarsym,n,vsp,def,vopts);
  1088. _mangledname:=nil;
  1089. end;
  1090. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1091. begin
  1092. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1093. end;
  1094. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1095. begin
  1096. tstaticvarsym(self).create(n,vsp,def,[]);
  1097. set_mangledname(mangled);
  1098. end;
  1099. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1100. begin
  1101. inherited ppuload(staticvarsym,ppufile);
  1102. if vo_has_mangledname in varoptions then
  1103. _mangledname:=stringdup(ppufile.getstring)
  1104. else
  1105. _mangledname:=nil;
  1106. end;
  1107. destructor tstaticvarsym.destroy;
  1108. begin
  1109. if assigned(_mangledname) then
  1110. begin
  1111. {$ifdef MEMDEBUG}
  1112. memmanglednames.start;
  1113. {$endif MEMDEBUG}
  1114. stringdispose(_mangledname);
  1115. {$ifdef MEMDEBUG}
  1116. memmanglednames.stop;
  1117. {$endif MEMDEBUG}
  1118. end;
  1119. inherited destroy;
  1120. end;
  1121. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1122. begin
  1123. inherited ppuwrite(ppufile);
  1124. if vo_has_mangledname in varoptions then
  1125. ppufile.putstring(_mangledname^);
  1126. ppufile.writeentry(ibstaticvarsym);
  1127. end;
  1128. function tstaticvarsym.mangledname:string;
  1129. var
  1130. prefix : string[2];
  1131. begin
  1132. if not assigned(_mangledname) then
  1133. begin
  1134. if (vo_is_typed_const in varoptions) then
  1135. prefix:='TC'
  1136. else
  1137. prefix:='U';
  1138. {$ifdef compress}
  1139. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1140. {$else}
  1141. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1142. {$endif}
  1143. end;
  1144. result:=_mangledname^;
  1145. end;
  1146. procedure tstaticvarsym.set_mangledname(const s:string);
  1147. begin
  1148. stringdispose(_mangledname);
  1149. {$ifdef compress}
  1150. _mangledname:=stringdup(minilzw_encode(s));
  1151. {$else}
  1152. _mangledname:=stringdup(s);
  1153. {$endif}
  1154. include(varoptions,vo_has_mangledname);
  1155. end;
  1156. {****************************************************************************
  1157. TLOCALVARSYM
  1158. ****************************************************************************}
  1159. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1160. begin
  1161. inherited create(localvarsym,n,vsp,def,vopts);
  1162. end;
  1163. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1164. begin
  1165. inherited ppuload(localvarsym,ppufile);
  1166. end;
  1167. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1168. begin
  1169. inherited ppuwrite(ppufile);
  1170. ppufile.writeentry(iblocalvarsym);
  1171. end;
  1172. {****************************************************************************
  1173. TPARAVARSYM
  1174. ****************************************************************************}
  1175. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1176. begin
  1177. inherited create(paravarsym,n,vsp,def,vopts);
  1178. if (vsp in [vs_var,vs_value,vs_const]) then
  1179. varstate := vs_initialised;
  1180. paranr:=nr;
  1181. paraloc[calleeside].init;
  1182. paraloc[callerside].init;
  1183. end;
  1184. destructor tparavarsym.destroy;
  1185. begin
  1186. paraloc[calleeside].done;
  1187. paraloc[callerside].done;
  1188. inherited destroy;
  1189. end;
  1190. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1191. var
  1192. b : byte;
  1193. begin
  1194. inherited ppuload(paravarsym,ppufile);
  1195. paranr:=ppufile.getword;
  1196. { The var state of parameter symbols is fixed after writing them so
  1197. we write them to the unit file.
  1198. This enables constant folding for inline procedures loaded from units
  1199. }
  1200. varstate:=tvarstate(ppufile.getbyte);
  1201. paraloc[calleeside].init;
  1202. paraloc[callerside].init;
  1203. if vo_has_explicit_paraloc in varoptions then
  1204. begin
  1205. paraloc[callerside].alignment:=ppufile.getbyte;
  1206. b:=ppufile.getbyte;
  1207. if b<>sizeof(paraloc[callerside].location^) then
  1208. internalerror(200411154);
  1209. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1210. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1211. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1212. end;
  1213. end;
  1214. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1215. var
  1216. oldintfcrc : boolean;
  1217. begin
  1218. inherited ppuwrite(ppufile);
  1219. ppufile.putword(paranr);
  1220. { The var state of parameter symbols is fixed after writing them so
  1221. we write them to the unit file.
  1222. This enables constant folding for inline procedures loaded from units
  1223. }
  1224. oldintfcrc:=ppufile.do_crc;
  1225. ppufile.do_crc:=false;
  1226. ppufile.putbyte(ord(varstate));
  1227. ppufile.do_crc:=oldintfcrc;
  1228. if vo_has_explicit_paraloc in varoptions then
  1229. begin
  1230. paraloc[callerside].check_simple_location;
  1231. ppufile.putbyte(sizeof(paraloc[callerside].alignment));
  1232. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1233. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1234. end;
  1235. ppufile.writeentry(ibparavarsym);
  1236. end;
  1237. {****************************************************************************
  1238. TABSOLUTEVARSYM
  1239. ****************************************************************************}
  1240. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1241. begin
  1242. inherited create(absolutevarsym,n,vs_value,def,[]);
  1243. ref:=nil;
  1244. end;
  1245. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1246. begin
  1247. inherited create(absolutevarsym,n,vs_value,def,[]);
  1248. ref:=_ref;
  1249. end;
  1250. destructor tabsolutevarsym.destroy;
  1251. begin
  1252. if assigned(ref) then
  1253. ref.free;
  1254. inherited destroy;
  1255. end;
  1256. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1257. begin
  1258. inherited ppuload(absolutevarsym,ppufile);
  1259. ref:=nil;
  1260. asmname:=nil;
  1261. abstyp:=absolutetyp(ppufile.getbyte);
  1262. {$ifdef i386}
  1263. absseg:=false;
  1264. {$endif i386}
  1265. case abstyp of
  1266. tovar :
  1267. ref:=ppufile.getpropaccesslist;
  1268. toasm :
  1269. asmname:=stringdup(ppufile.getstring);
  1270. toaddr :
  1271. begin
  1272. addroffset:=ppufile.getaword;
  1273. {$ifdef i386}
  1274. absseg:=boolean(ppufile.getbyte);
  1275. {$endif i386}
  1276. end;
  1277. end;
  1278. end;
  1279. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1280. begin
  1281. inherited ppuwrite(ppufile);
  1282. ppufile.putbyte(byte(abstyp));
  1283. case abstyp of
  1284. tovar :
  1285. ppufile.putpropaccesslist(ref);
  1286. toasm :
  1287. ppufile.putstring(asmname^);
  1288. toaddr :
  1289. begin
  1290. ppufile.putaword(addroffset);
  1291. {$ifdef i386}
  1292. ppufile.putbyte(byte(absseg));
  1293. {$endif i386}
  1294. end;
  1295. end;
  1296. ppufile.writeentry(ibabsolutevarsym);
  1297. end;
  1298. procedure tabsolutevarsym.buildderef;
  1299. begin
  1300. inherited buildderef;
  1301. if (abstyp=tovar) then
  1302. ref.buildderef;
  1303. end;
  1304. procedure tabsolutevarsym.deref;
  1305. begin
  1306. inherited deref;
  1307. { own absolute deref }
  1308. if (abstyp=tovar) then
  1309. ref.resolve;
  1310. end;
  1311. function tabsolutevarsym.mangledname : string;
  1312. begin
  1313. case abstyp of
  1314. toasm :
  1315. mangledname:=asmname^;
  1316. toaddr :
  1317. mangledname:='$'+tostr(addroffset);
  1318. else
  1319. internalerror(200411062);
  1320. end;
  1321. end;
  1322. {****************************************************************************
  1323. TCONSTSYM
  1324. ****************************************************************************}
  1325. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1326. begin
  1327. inherited create(constsym,n);
  1328. fillchar(value, sizeof(value), #0);
  1329. consttyp:=t;
  1330. value.valueord:=v;
  1331. constdef:=def;
  1332. end;
  1333. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1334. begin
  1335. inherited create(constsym,n);
  1336. fillchar(value, sizeof(value), #0);
  1337. consttyp:=t;
  1338. value.valueordptr:=v;
  1339. constdef:=def;
  1340. end;
  1341. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1342. begin
  1343. inherited create(constsym,n);
  1344. fillchar(value, sizeof(value), #0);
  1345. consttyp:=t;
  1346. value.valueptr:=v;
  1347. constdef:=def;
  1348. end;
  1349. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1350. begin
  1351. inherited create(constsym,n);
  1352. fillchar(value, sizeof(value), #0);
  1353. consttyp:=t;
  1354. value.valueptr:=str;
  1355. constdef:=nil;
  1356. value.len:=l;
  1357. end;
  1358. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1359. begin
  1360. inherited create(constsym,n);
  1361. fillchar(value, sizeof(value), #0);
  1362. consttyp:=t;
  1363. pcompilerwidestring(value.valueptr):=pw;
  1364. constdef:=nil;
  1365. value.len:=getlengthwidestring(pw);
  1366. end;
  1367. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1368. var
  1369. pd : pbestreal;
  1370. ps : pnormalset;
  1371. pc : pchar;
  1372. pw : pcompilerwidestring;
  1373. i : longint;
  1374. begin
  1375. inherited ppuload(constsym,ppufile);
  1376. constdef:=nil;
  1377. consttyp:=tconsttyp(ppufile.getbyte);
  1378. fillchar(value, sizeof(value), #0);
  1379. case consttyp of
  1380. constord :
  1381. begin
  1382. ppufile.getderef(constdefderef);
  1383. value.valueord:=ppufile.getexprint;
  1384. end;
  1385. constpointer :
  1386. begin
  1387. ppufile.getderef(constdefderef);
  1388. value.valueordptr:=ppufile.getptruint;
  1389. end;
  1390. constwstring :
  1391. begin
  1392. initwidestring(pw);
  1393. setlengthwidestring(pw,ppufile.getlongint);
  1394. pw^.len:=pw^.maxlen;
  1395. { don't use getdata, because the compilerwidechars may have to
  1396. be byteswapped
  1397. }
  1398. {$if sizeof(tcompilerwidechar) = 2}
  1399. for i:=0 to pw^.len-1 do
  1400. pw^.data[i]:=ppufile.getword;
  1401. {$elseif sizeof(tcompilerwidechar) = 4}
  1402. for i:=0 to pw^.len-1 do
  1403. pw^.data[i]:=cardinal(ppufile.getlongint);
  1404. {$else}
  1405. {$error Unsupported tcompilerwidechar size}
  1406. {$endif}
  1407. pcompilerwidestring(value.valueptr):=pw;
  1408. end;
  1409. conststring,
  1410. constresourcestring :
  1411. begin
  1412. value.len:=ppufile.getlongint;
  1413. getmem(pc,value.len+1);
  1414. ppufile.getdata(pc^,value.len);
  1415. pc[value.len]:=#0;
  1416. value.valueptr:=pc;
  1417. end;
  1418. constreal :
  1419. begin
  1420. new(pd);
  1421. pd^:=ppufile.getreal;
  1422. value.valueptr:=pd;
  1423. end;
  1424. constset :
  1425. begin
  1426. ppufile.getderef(constdefderef);
  1427. new(ps);
  1428. ppufile.getnormalset(ps^);
  1429. value.valueptr:=ps;
  1430. end;
  1431. constguid :
  1432. begin
  1433. new(pguid(value.valueptr));
  1434. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1435. end;
  1436. constnil : ;
  1437. else
  1438. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1439. end;
  1440. end;
  1441. destructor tconstsym.destroy;
  1442. begin
  1443. case consttyp of
  1444. conststring,
  1445. constresourcestring :
  1446. freemem(pchar(value.valueptr),value.len+1);
  1447. constwstring :
  1448. donewidestring(pcompilerwidestring(value.valueptr));
  1449. constreal :
  1450. dispose(pbestreal(value.valueptr));
  1451. constset :
  1452. dispose(pnormalset(value.valueptr));
  1453. constguid :
  1454. dispose(pguid(value.valueptr));
  1455. end;
  1456. inherited destroy;
  1457. end;
  1458. procedure tconstsym.buildderef;
  1459. begin
  1460. if consttyp in [constord,constpointer,constset] then
  1461. constdefderef.build(constdef);
  1462. end;
  1463. procedure tconstsym.deref;
  1464. begin
  1465. if consttyp in [constord,constpointer,constset] then
  1466. constdef:=tdef(constdefderef.resolve);
  1467. end;
  1468. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1469. begin
  1470. inherited ppuwrite(ppufile);
  1471. ppufile.putbyte(byte(consttyp));
  1472. case consttyp of
  1473. constnil : ;
  1474. constord :
  1475. begin
  1476. ppufile.putderef(constdefderef);
  1477. ppufile.putexprint(value.valueord);
  1478. end;
  1479. constpointer :
  1480. begin
  1481. ppufile.putderef(constdefderef);
  1482. ppufile.putptruint(value.valueordptr);
  1483. end;
  1484. constwstring :
  1485. begin
  1486. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1487. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1488. end;
  1489. conststring,
  1490. constresourcestring :
  1491. begin
  1492. ppufile.putlongint(value.len);
  1493. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1494. end;
  1495. constreal :
  1496. ppufile.putreal(pbestreal(value.valueptr)^);
  1497. constset :
  1498. begin
  1499. ppufile.putderef(constdefderef);
  1500. ppufile.putnormalset(value.valueptr^);
  1501. end;
  1502. constguid :
  1503. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1504. else
  1505. internalerror(13);
  1506. end;
  1507. ppufile.writeentry(ibconstsym);
  1508. end;
  1509. {****************************************************************************
  1510. TENUMSYM
  1511. ****************************************************************************}
  1512. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1513. begin
  1514. inherited create(enumsym,n);
  1515. definition:=def;
  1516. value:=v;
  1517. { First entry? Then we need to set the minval }
  1518. if def.firstenum=nil then
  1519. begin
  1520. if v>0 then
  1521. def.has_jumps:=true;
  1522. def.setmin(v);
  1523. def.setmax(v);
  1524. end
  1525. else
  1526. begin
  1527. { check for jumps }
  1528. if v>def.max+1 then
  1529. def.has_jumps:=true;
  1530. { update low and high }
  1531. if def.min>v then
  1532. def.setmin(v);
  1533. if def.max<v then
  1534. def.setmax(v);
  1535. end;
  1536. order;
  1537. end;
  1538. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1539. begin
  1540. inherited ppuload(enumsym,ppufile);
  1541. ppufile.getderef(definitionderef);
  1542. value:=ppufile.getlongint;
  1543. nextenum := Nil;
  1544. end;
  1545. procedure tenumsym.buildderef;
  1546. begin
  1547. definitionderef.build(definition);
  1548. end;
  1549. procedure tenumsym.deref;
  1550. begin
  1551. definition:=tenumdef(definitionderef.resolve);
  1552. order;
  1553. end;
  1554. procedure tenumsym.order;
  1555. var
  1556. sym : tenumsym;
  1557. begin
  1558. sym := tenumsym(definition.firstenum);
  1559. if sym = nil then
  1560. begin
  1561. definition.firstenum := self;
  1562. nextenum := nil;
  1563. exit;
  1564. end;
  1565. { reorder the symbols in increasing value }
  1566. if value < sym.value then
  1567. begin
  1568. nextenum := sym;
  1569. definition.firstenum := self;
  1570. end
  1571. else
  1572. begin
  1573. while (sym.value <= value) and assigned(sym.nextenum) do
  1574. sym := sym.nextenum;
  1575. nextenum := sym.nextenum;
  1576. sym.nextenum := self;
  1577. end;
  1578. end;
  1579. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1580. begin
  1581. inherited ppuwrite(ppufile);
  1582. ppufile.putderef(definitionderef);
  1583. ppufile.putlongint(value);
  1584. ppufile.writeentry(ibenumsym);
  1585. end;
  1586. {****************************************************************************
  1587. TTYPESYM
  1588. ****************************************************************************}
  1589. constructor ttypesym.create(const n : string;def:tdef);
  1590. begin
  1591. inherited create(typesym,n);
  1592. typedef:=def;
  1593. { register the typesym for the definition }
  1594. if assigned(typedef) and
  1595. (typedef.typ<>errordef) and
  1596. not(assigned(typedef.typesym)) then
  1597. typedef.typesym:=self;
  1598. end;
  1599. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1600. begin
  1601. inherited ppuload(typesym,ppufile);
  1602. ppufile.getderef(typedefderef);
  1603. end;
  1604. procedure ttypesym.buildderef;
  1605. begin
  1606. typedefderef.build(typedef);
  1607. end;
  1608. procedure ttypesym.deref;
  1609. begin
  1610. typedef:=tdef(typedefderef.resolve);
  1611. end;
  1612. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1613. begin
  1614. inherited ppuwrite(ppufile);
  1615. ppufile.putderef(typedefderef);
  1616. ppufile.writeentry(ibtypesym);
  1617. end;
  1618. {****************************************************************************
  1619. TSYSSYM
  1620. ****************************************************************************}
  1621. constructor tsyssym.create(const n : string;l : longint);
  1622. begin
  1623. inherited create(syssym,n);
  1624. number:=l;
  1625. end;
  1626. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1627. begin
  1628. inherited ppuload(syssym,ppufile);
  1629. number:=ppufile.getlongint;
  1630. end;
  1631. destructor tsyssym.destroy;
  1632. begin
  1633. inherited destroy;
  1634. end;
  1635. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1636. begin
  1637. inherited ppuwrite(ppufile);
  1638. ppufile.putlongint(number);
  1639. ppufile.writeentry(ibsyssym);
  1640. end;
  1641. {*****************************************************************************
  1642. TMacro
  1643. *****************************************************************************}
  1644. constructor tmacro.create(const n : string);
  1645. begin
  1646. inherited create(macrosym,n);
  1647. owner:=nil;
  1648. defined:=false;
  1649. is_used:=false;
  1650. is_compiler_var:=false;
  1651. buftext:=nil;
  1652. buflen:=0;
  1653. end;
  1654. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1655. begin
  1656. inherited ppuload(macrosym,ppufile);
  1657. defined:=boolean(ppufile.getbyte);
  1658. is_compiler_var:=boolean(ppufile.getbyte);
  1659. is_used:=false;
  1660. buflen:= ppufile.getlongint;
  1661. if buflen > 0 then
  1662. begin
  1663. getmem(buftext, buflen);
  1664. ppufile.getdata(buftext^, buflen)
  1665. end
  1666. else
  1667. buftext:=nil;
  1668. end;
  1669. destructor tmacro.destroy;
  1670. begin
  1671. if assigned(buftext) then
  1672. freemem(buftext);
  1673. inherited destroy;
  1674. end;
  1675. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1676. begin
  1677. inherited ppuwrite(ppufile);
  1678. ppufile.putbyte(byte(defined));
  1679. ppufile.putbyte(byte(is_compiler_var));
  1680. ppufile.putlongint(buflen);
  1681. if buflen > 0 then
  1682. ppufile.putdata(buftext^,buflen);
  1683. ppufile.writeentry(ibmacrosym);
  1684. end;
  1685. function tmacro.GetCopy:tmacro;
  1686. var
  1687. p : tmacro;
  1688. begin
  1689. p:=tmacro.create(realname);
  1690. p.defined:=defined;
  1691. p.is_used:=is_used;
  1692. p.is_compiler_var:=is_compiler_var;
  1693. p.buflen:=buflen;
  1694. if assigned(buftext) then
  1695. begin
  1696. getmem(p.buftext,buflen);
  1697. move(buftext^,p.buftext^,buflen);
  1698. end;
  1699. Result:=p;
  1700. end;
  1701. end.