symsym.pas 58 KB

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