symsym.pas 61 KB

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