symsym.pas 58 KB

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