symsym.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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. procedure setvardef(def:tdef);
  126. _vardef : tdef;
  127. vardefderef : tderef;
  128. public
  129. property vardef: tdef read _vardef write setvardef;
  130. end;
  131. tfieldvarsym = class(tabstractvarsym)
  132. fieldoffset : aint; { offset in record/object }
  133. {$ifdef support_llvm}
  134. { the llvm version of the record does not support variants, }
  135. { so the llvm equivalent field may not be at the exact same }
  136. { offset -> store the difference (bits for bitpacked records, }
  137. { bytes otherwise) }
  138. offsetfromllvmfield : aint;
  139. { number of the closest field in the llvm definition }
  140. llvmfieldnr : longint;
  141. {$endif support_llvm}
  142. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  143. constructor ppuload(ppufile:tcompilerppufile);
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. function mangledname:string;override;
  146. end;
  147. tabstractnormalvarsym = class(tabstractvarsym)
  148. defaultconstsym : tsym;
  149. defaultconstsymderef : tderef;
  150. localloc : TLocation; { register/reference for local var }
  151. initialloc : TLocation; { initial location so it can still be initialized later after the location was changed by SSA }
  152. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  153. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;
  155. procedure buildderef;override;
  156. procedure deref;override;
  157. end;
  158. tlocalvarsym = class(tabstractnormalvarsym)
  159. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  160. constructor ppuload(ppufile:tcompilerppufile);
  161. procedure ppuwrite(ppufile:tcompilerppufile);override;
  162. end;
  163. tparavarsym = class(tabstractnormalvarsym)
  164. paraloc : array[tcallercallee] of TCGPara;
  165. paranr : word; { position of this parameter }
  166. {$ifdef EXTDEBUG}
  167. eqval : tequaltype;
  168. {$endif EXTDEBUG}
  169. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  170. constructor ppuload(ppufile:tcompilerppufile);
  171. destructor destroy;override;
  172. procedure ppuwrite(ppufile:tcompilerppufile);override;
  173. end;
  174. tstaticvarsym = class(tabstractnormalvarsym)
  175. private
  176. _mangledname : pshortstring;
  177. public
  178. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  179. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);
  180. constructor create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  181. constructor ppuload(ppufile:tcompilerppufile);
  182. destructor destroy;override;
  183. procedure ppuwrite(ppufile:tcompilerppufile);override;
  184. function mangledname:string;override;
  185. procedure set_mangledname(const s:string);
  186. end;
  187. tabsolutevarsym = class(tabstractvarsym)
  188. public
  189. abstyp : absolutetyp;
  190. {$ifdef i386}
  191. absseg : boolean;
  192. {$endif i386}
  193. asmname : pshortstring;
  194. addroffset : aint;
  195. ref : tpropaccesslist;
  196. constructor create(const n : string;def:tdef);
  197. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  198. destructor destroy;override;
  199. constructor ppuload(ppufile:tcompilerppufile);
  200. procedure buildderef;override;
  201. procedure deref;override;
  202. function mangledname : string;override;
  203. procedure ppuwrite(ppufile:tcompilerppufile);override;
  204. end;
  205. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  206. tpropertysym = class(Tstoredsym)
  207. propoptions : tpropertyoptions;
  208. overridenpropsym : tpropertysym;
  209. overridenpropsymderef : tderef;
  210. propdef : tdef;
  211. propdefderef : tderef;
  212. indexdef : tdef;
  213. indexdefderef : tderef;
  214. index,
  215. default : longint;
  216. propaccesslist : array[tpropaccesslisttypes] of tpropaccesslist;
  217. constructor create(const n : string);
  218. destructor destroy;override;
  219. constructor ppuload(ppufile:tcompilerppufile);
  220. function getsize : aint;
  221. procedure ppuwrite(ppufile:tcompilerppufile);override;
  222. procedure buildderef;override;
  223. procedure deref;override;
  224. end;
  225. tconstvalue = record
  226. case integer of
  227. 0: (valueord : tconstexprint);
  228. 1: (valueordptr : tconstptruint);
  229. 2: (valueptr : pointer; len : longint);
  230. end;
  231. tconstsym = class(tstoredsym)
  232. constdef : tdef;
  233. constdefderef : tderef;
  234. consttyp : tconsttyp;
  235. value : tconstvalue;
  236. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  237. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  238. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  239. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  240. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  241. constructor ppuload(ppufile:tcompilerppufile);
  242. destructor destroy;override;
  243. procedure buildderef;override;
  244. procedure deref;override;
  245. procedure ppuwrite(ppufile:tcompilerppufile);override;
  246. end;
  247. tenumsym = class(Tstoredsym)
  248. value : longint;
  249. definition : tenumdef;
  250. definitionderef : tderef;
  251. nextenum : tenumsym;
  252. constructor create(const n : string;def : tenumdef;v : longint);
  253. constructor ppuload(ppufile:tcompilerppufile);
  254. procedure ppuwrite(ppufile:tcompilerppufile);override;
  255. procedure buildderef;override;
  256. procedure deref;override;
  257. procedure order;
  258. end;
  259. tsyssym = class(Tstoredsym)
  260. number : longint;
  261. constructor create(const n : string;l : longint);
  262. constructor ppuload(ppufile:tcompilerppufile);
  263. destructor destroy;override;
  264. procedure ppuwrite(ppufile:tcompilerppufile);override;
  265. end;
  266. const
  267. maxmacrolen=16*1024;
  268. type
  269. pmacrobuffer = ^tmacrobuffer;
  270. tmacrobuffer = array[0..maxmacrolen-1] of char;
  271. tmacro = class(tstoredsym)
  272. {Normally true, but false when a previously defined macro is undef-ed}
  273. defined : boolean;
  274. {True if this is a mac style compiler variable, in which case no macro
  275. substitutions shall be done.}
  276. is_compiler_var : boolean;
  277. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  278. {e. g. an IFDEF which returns false, will not be registered as used,}
  279. {since there is no place to register its use. }
  280. is_used : boolean;
  281. buftext : pchar;
  282. buflen : longint;
  283. constructor create(const n : string);
  284. constructor ppuload(ppufile:tcompilerppufile);
  285. procedure ppuwrite(ppufile:tcompilerppufile);override;
  286. destructor destroy;override;
  287. function GetCopy:tmacro;
  288. end;
  289. var
  290. generrorsym : tsym;
  291. implementation
  292. uses
  293. { global }
  294. verbose,
  295. { target }
  296. systems,
  297. { symtable }
  298. defutil,symtable,
  299. fmodule,
  300. { tree }
  301. node,
  302. { aasm }
  303. aasmtai,aasmdata,
  304. { codegen }
  305. paramgr,
  306. procinfo
  307. ;
  308. {****************************************************************************
  309. Helpers
  310. ****************************************************************************}
  311. {****************************************************************************
  312. TSYM (base for all symtypes)
  313. ****************************************************************************}
  314. constructor tstoredsym.create(st:tsymtyp;const n : string);
  315. begin
  316. inherited create(st,n);
  317. { Register in current_module }
  318. if assigned(current_module) then
  319. begin
  320. current_module.symlist.Add(self);
  321. SymId:=current_module.symlist.Count-1;
  322. end;
  323. end;
  324. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  325. begin
  326. SymId:=ppufile.getlongint;
  327. inherited Create(st,ppufile.getstring);
  328. { Register symbol }
  329. current_module.symlist[SymId]:=self;
  330. ppufile.getposinfo(fileinfo);
  331. visibility:=tvisibility(ppufile.getbyte);
  332. ppufile.getsmallset(symoptions);
  333. end;
  334. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  335. begin
  336. ppufile.putlongint(SymId);
  337. ppufile.putstring(realname);
  338. ppufile.putposinfo(fileinfo);
  339. ppufile.putbyte(byte(visibility));
  340. ppufile.putsmallset(symoptions);
  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_byprocvardef(d:Tprocvardef):Tprocdef;
  583. var
  584. i : longint;
  585. bestpd,
  586. pd : tprocdef;
  587. eq,besteq : tequaltype;
  588. begin
  589. { This function will return the pprocdef of pprocsym that
  590. is the best match for procvardef. When there are multiple
  591. matches it returns nil.}
  592. result:=nil;
  593. bestpd:=nil;
  594. besteq:=te_incompatible;
  595. for i:=0 to ProcdefList.Count-1 do
  596. begin
  597. pd:=tprocdef(ProcdefList[i]);
  598. eq:=proc_to_procvar_equal(pd,d);
  599. if eq>=te_equal then
  600. begin
  601. { multiple procvars with the same equal level }
  602. if assigned(bestpd) and
  603. (besteq=eq) then
  604. exit;
  605. if eq>besteq then
  606. begin
  607. besteq:=eq;
  608. bestpd:=pd;
  609. end;
  610. end;
  611. end;
  612. result:=bestpd;
  613. end;
  614. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  615. var
  616. paraidx,
  617. i : longint;
  618. bestpd,
  619. hpd,
  620. pd : tprocdef;
  621. convtyp : tconverttype;
  622. eq : tequaltype;
  623. begin
  624. { This function will return the pprocdef of pprocsym that
  625. is the best match for procvardef. When there are multiple
  626. matches it returns nil.}
  627. result:=nil;
  628. bestpd:=nil;
  629. besteq:=te_incompatible;
  630. for i:=0 to ProcdefList.Count-1 do
  631. begin
  632. pd:=tprocdef(ProcdefList[i]);
  633. if equal_defs(todef,pd.returndef) and
  634. { the result type must be always really equal and not an alias,
  635. if you mess with this code, check tw4093 }
  636. ((todef=pd.returndef) or
  637. (
  638. not(df_unique in todef.defoptions) and
  639. not(df_unique in pd.returndef.defoptions)
  640. )
  641. ) then
  642. begin
  643. paraidx:=0;
  644. { ignore vs_hidden parameters }
  645. while (paraidx<pd.paras.count) and
  646. assigned(pd.paras[paraidx]) and
  647. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  648. inc(paraidx);
  649. if (paraidx<pd.paras.count) and
  650. assigned(pd.paras[paraidx]) then
  651. begin
  652. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  653. { alias? if yes, only l1 choice,
  654. if you mess with this code, check tw4093 }
  655. if (eq=te_exact) and
  656. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  657. ((df_unique in fromdef.defoptions) or
  658. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  659. eq:=te_convert_l1;
  660. if eq=te_exact then
  661. begin
  662. besteq:=eq;
  663. result:=pd;
  664. exit;
  665. end;
  666. if eq>besteq then
  667. begin
  668. bestpd:=pd;
  669. besteq:=eq;
  670. end;
  671. end;
  672. end;
  673. end;
  674. result:=bestpd;
  675. end;
  676. {****************************************************************************
  677. TERRORSYM
  678. ****************************************************************************}
  679. constructor terrorsym.create;
  680. begin
  681. inherited create(errorsym,'');
  682. end;
  683. {****************************************************************************
  684. TPROPERTYSYM
  685. ****************************************************************************}
  686. constructor tpropertysym.create(const n : string);
  687. var
  688. pap : tpropaccesslisttypes;
  689. begin
  690. inherited create(propertysym,n);
  691. propoptions:=[];
  692. index:=0;
  693. default:=0;
  694. propdef:=nil;
  695. indexdef:=nil;
  696. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  697. propaccesslist[pap]:=tpropaccesslist.create;
  698. end;
  699. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  700. var
  701. pap : tpropaccesslisttypes;
  702. begin
  703. inherited ppuload(propertysym,ppufile);
  704. ppufile.getsmallset(propoptions);
  705. ppufile.getderef(overridenpropsymderef);
  706. ppufile.getderef(propdefderef);
  707. index:=ppufile.getlongint;
  708. default:=ppufile.getlongint;
  709. ppufile.getderef(indexdefderef);
  710. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  711. propaccesslist[pap]:=ppufile.getpropaccesslist;
  712. end;
  713. destructor tpropertysym.destroy;
  714. var
  715. pap : tpropaccesslisttypes;
  716. begin
  717. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  718. propaccesslist[pap].free;
  719. inherited destroy;
  720. end;
  721. procedure tpropertysym.buildderef;
  722. var
  723. pap : tpropaccesslisttypes;
  724. begin
  725. overridenpropsymderef.build(overridenpropsym);
  726. propdefderef.build(propdef);
  727. indexdefderef.build(indexdef);
  728. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  729. propaccesslist[pap].buildderef;
  730. end;
  731. procedure tpropertysym.deref;
  732. var
  733. pap : tpropaccesslisttypes;
  734. begin
  735. overridenpropsym:=tpropertysym(overridenpropsymderef.resolve);
  736. indexdef:=tdef(indexdefderef.resolve);
  737. propdef:=tdef(propdefderef.resolve);
  738. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  739. propaccesslist[pap].resolve;
  740. end;
  741. function tpropertysym.getsize : aint;
  742. begin
  743. getsize:=0;
  744. end;
  745. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  746. var
  747. pap : tpropaccesslisttypes;
  748. begin
  749. inherited ppuwrite(ppufile);
  750. ppufile.putsmallset(propoptions);
  751. ppufile.putderef(overridenpropsymderef);
  752. ppufile.putderef(propdefderef);
  753. ppufile.putlongint(index);
  754. ppufile.putlongint(default);
  755. ppufile.putderef(indexdefderef);
  756. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  757. ppufile.putpropaccesslist(propaccesslist[pap]);
  758. ppufile.writeentry(ibpropertysym);
  759. end;
  760. {****************************************************************************
  761. TABSTRACTVARSYM
  762. ****************************************************************************}
  763. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  764. begin
  765. inherited create(st,n);
  766. vardef:=def;
  767. varspez:=vsp;
  768. varstate:=vs_declared;
  769. varoptions:=vopts;
  770. end;
  771. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  772. begin
  773. inherited ppuload(st,ppufile);
  774. varstate:=vs_readwritten;
  775. varspez:=tvarspez(ppufile.getbyte);
  776. varregable:=tvarregable(ppufile.getbyte);
  777. addr_taken:=boolean(ppufile.getbyte);
  778. ppufile.getderef(vardefderef);
  779. ppufile.getsmallset(varoptions);
  780. end;
  781. destructor tabstractvarsym.destroy;
  782. begin
  783. if assigned(notifications) then
  784. notifications.destroy;
  785. inherited destroy;
  786. end;
  787. procedure tabstractvarsym.buildderef;
  788. begin
  789. vardefderef.build(vardef);
  790. end;
  791. procedure tabstractvarsym.deref;
  792. var
  793. oldvarregable: tvarregable;
  794. begin
  795. { setting the vardef also updates varregable. We just loaded this }
  796. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  797. { tw7817b.pp: the address is taken of a local variable in an }
  798. { inlined procedure -> must remain non-regable when inlining) }
  799. oldvarregable:=varregable;
  800. vardef:=tdef(vardefderef.resolve);
  801. varregable:=oldvarregable;
  802. end;
  803. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  804. var
  805. oldintfcrc : boolean;
  806. begin
  807. inherited ppuwrite(ppufile);
  808. ppufile.putbyte(byte(varspez));
  809. oldintfcrc:=ppufile.do_crc;
  810. ppufile.do_crc:=false;
  811. ppufile.putbyte(byte(varregable));
  812. ppufile.putbyte(byte(addr_taken));
  813. ppufile.do_crc:=oldintfcrc;
  814. ppufile.putderef(vardefderef);
  815. ppufile.putsmallset(varoptions);
  816. end;
  817. function tabstractvarsym.getsize : aint;
  818. begin
  819. if assigned(vardef) and
  820. ((vardef.typ<>arraydef) or
  821. is_dynamic_array(vardef) or
  822. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  823. result:=vardef.size
  824. else
  825. result:=0;
  826. end;
  827. function tabstractvarsym.getpackedbitsize : longint;
  828. begin
  829. { bitpacking is only done for ordinals }
  830. if not is_ordinal(vardef) then
  831. internalerror(2006082010);
  832. result:=vardef.packedbitsize;
  833. end;
  834. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  835. begin
  836. { Register variables are not allowed in the following cases:
  837. - regvars are disabled
  838. - exceptions are used (after an exception is raised the contents of the
  839. registers is not valid anymore)
  840. - it has a local copy
  841. - the value needs to be in memory (i.e. reference counted) }
  842. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  843. not(pi_has_assembler_block in current_procinfo.flags) and
  844. not(pi_uses_exceptions in current_procinfo.flags) and
  845. not(vo_has_local_copy in varoptions) and
  846. ((refpara and
  847. (varregable <> vr_none)) or
  848. (not refpara and
  849. not(varregable in [vr_none,vr_addr])))
  850. {$if not defined(powerpc) and not defined(powerpc64)}
  851. and ((vardef.typ <> recorddef) or
  852. (varregable = vr_addr) or
  853. not(varstate in [vs_written,vs_readwritten]));
  854. {$endif}
  855. end;
  856. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  857. var n:Tnotification;
  858. begin
  859. if assigned(notifications) then
  860. begin
  861. n:=Tnotification(notifications.first);
  862. while assigned(n) do
  863. begin
  864. if what in n.flags then
  865. n.callback(what,self);
  866. n:=Tnotification(n.next);
  867. end;
  868. end;
  869. end;
  870. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  871. Tnotification_callback):cardinal;
  872. var n:Tnotification;
  873. begin
  874. if not assigned(notifications) then
  875. notifications:=Tlinkedlist.create;
  876. n:=Tnotification.create(flags,callback);
  877. register_notification:=n.id;
  878. notifications.concat(n);
  879. end;
  880. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  881. var n:Tnotification;
  882. begin
  883. if not assigned(notifications) then
  884. internalerror(200212311)
  885. else
  886. begin
  887. n:=Tnotification(notifications.first);
  888. while assigned(n) do
  889. begin
  890. if n.id=id then
  891. begin
  892. notifications.remove(n);
  893. n.destroy;
  894. exit;
  895. end;
  896. n:=Tnotification(n.next);
  897. end;
  898. internalerror(200212311)
  899. end;
  900. end;
  901. procedure tabstractvarsym.setvardef(def:tdef);
  902. begin
  903. _vardef := def;
  904. { can we load the value into a register ? }
  905. if not assigned(owner) or
  906. (owner.symtabletype in [localsymtable,parasymtable]) or
  907. (
  908. (owner.symtabletype=staticsymtable) and
  909. not(cs_create_pic in current_settings.moduleswitches)
  910. ) then
  911. begin
  912. if tstoreddef(vardef).is_intregable then
  913. varregable:=vr_intreg
  914. else
  915. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  916. if {(
  917. not assigned(owner) or
  918. (owner.symtabletype<>staticsymtable)
  919. ) and }
  920. tstoreddef(vardef).is_fpuregable then
  921. begin
  922. {$ifdef x86}
  923. if use_sse(vardef) then
  924. varregable:=vr_mmreg
  925. else
  926. {$else x86}
  927. varregable:=vr_fpureg;
  928. {$endif x86}
  929. end;
  930. end;
  931. end;
  932. {****************************************************************************
  933. TFIELDVARSYM
  934. ****************************************************************************}
  935. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  936. begin
  937. inherited create(fieldvarsym,n,vsp,def,vopts);
  938. fieldoffset:=-1;
  939. end;
  940. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  941. begin
  942. inherited ppuload(fieldvarsym,ppufile);
  943. fieldoffset:=ppufile.getaint;
  944. end;
  945. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  946. begin
  947. inherited ppuwrite(ppufile);
  948. ppufile.putaint(fieldoffset);
  949. ppufile.writeentry(ibfieldvarsym);
  950. end;
  951. function tfieldvarsym.mangledname:string;
  952. var
  953. srsym : tsym;
  954. srsymtable : tsymtable;
  955. begin
  956. if sp_static in symoptions then
  957. begin
  958. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  959. result:=srsym.mangledname
  960. { when generating the debug info for the module in which the }
  961. { symbol is defined, the localsymtable of that module is }
  962. { already popped from the symtablestack }
  963. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  964. result:=srsym.mangledname
  965. else
  966. internalerror(2007012501);
  967. end
  968. else
  969. result:=inherited mangledname;
  970. end;
  971. {****************************************************************************
  972. TABSTRACTNORMALVARSYM
  973. ****************************************************************************}
  974. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  975. begin
  976. inherited create(st,n,vsp,def,vopts);
  977. fillchar(localloc,sizeof(localloc),0);
  978. fillchar(initialloc,sizeof(initialloc),0);
  979. defaultconstsym:=nil;
  980. end;
  981. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  982. begin
  983. inherited ppuload(st,ppufile);
  984. fillchar(localloc,sizeof(localloc),0);
  985. fillchar(initialloc,sizeof(initialloc),0);
  986. ppufile.getderef(defaultconstsymderef);
  987. end;
  988. procedure tabstractnormalvarsym.buildderef;
  989. begin
  990. inherited buildderef;
  991. defaultconstsymderef.build(defaultconstsym);
  992. end;
  993. procedure tabstractnormalvarsym.deref;
  994. begin
  995. inherited deref;
  996. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  997. end;
  998. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  999. begin
  1000. inherited ppuwrite(ppufile);
  1001. ppufile.putderef(defaultconstsymderef);
  1002. end;
  1003. {****************************************************************************
  1004. Tstaticvarsym
  1005. ****************************************************************************}
  1006. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1007. begin
  1008. inherited create(staticvarsym,n,vsp,def,vopts);
  1009. _mangledname:=nil;
  1010. end;
  1011. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1012. begin
  1013. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1014. end;
  1015. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1016. begin
  1017. tstaticvarsym(self).create(n,vsp,def,[]);
  1018. set_mangledname(mangled);
  1019. end;
  1020. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1021. begin
  1022. inherited ppuload(staticvarsym,ppufile);
  1023. if vo_has_mangledname in varoptions then
  1024. _mangledname:=stringdup(ppufile.getstring)
  1025. else
  1026. _mangledname:=nil;
  1027. end;
  1028. destructor tstaticvarsym.destroy;
  1029. begin
  1030. if assigned(_mangledname) then
  1031. begin
  1032. {$ifdef MEMDEBUG}
  1033. memmanglednames.start;
  1034. {$endif MEMDEBUG}
  1035. stringdispose(_mangledname);
  1036. {$ifdef MEMDEBUG}
  1037. memmanglednames.stop;
  1038. {$endif MEMDEBUG}
  1039. end;
  1040. inherited destroy;
  1041. end;
  1042. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1043. begin
  1044. inherited ppuwrite(ppufile);
  1045. if vo_has_mangledname in varoptions then
  1046. ppufile.putstring(_mangledname^);
  1047. ppufile.writeentry(ibstaticvarsym);
  1048. end;
  1049. function tstaticvarsym.mangledname:string;
  1050. var
  1051. prefix : string[2];
  1052. begin
  1053. if not assigned(_mangledname) then
  1054. begin
  1055. if (vo_is_typed_const in varoptions) then
  1056. prefix:='TC'
  1057. else
  1058. prefix:='U';
  1059. {$ifdef compress}
  1060. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1061. {$else}
  1062. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1063. {$endif}
  1064. end;
  1065. result:=_mangledname^;
  1066. end;
  1067. procedure tstaticvarsym.set_mangledname(const s:string);
  1068. begin
  1069. stringdispose(_mangledname);
  1070. {$ifdef compress}
  1071. _mangledname:=stringdup(minilzw_encode(s));
  1072. {$else}
  1073. _mangledname:=stringdup(s);
  1074. {$endif}
  1075. include(varoptions,vo_has_mangledname);
  1076. end;
  1077. {****************************************************************************
  1078. TLOCALVARSYM
  1079. ****************************************************************************}
  1080. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1081. begin
  1082. inherited create(localvarsym,n,vsp,def,vopts);
  1083. end;
  1084. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1085. begin
  1086. inherited ppuload(localvarsym,ppufile);
  1087. end;
  1088. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1089. begin
  1090. inherited ppuwrite(ppufile);
  1091. ppufile.writeentry(iblocalvarsym);
  1092. end;
  1093. {****************************************************************************
  1094. TPARAVARSYM
  1095. ****************************************************************************}
  1096. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1097. begin
  1098. inherited create(paravarsym,n,vsp,def,vopts);
  1099. if (vsp in [vs_var,vs_value,vs_const]) then
  1100. varstate := vs_initialised;
  1101. paranr:=nr;
  1102. paraloc[calleeside].init;
  1103. paraloc[callerside].init;
  1104. end;
  1105. destructor tparavarsym.destroy;
  1106. begin
  1107. paraloc[calleeside].done;
  1108. paraloc[callerside].done;
  1109. inherited destroy;
  1110. end;
  1111. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1112. var
  1113. b : byte;
  1114. begin
  1115. inherited ppuload(paravarsym,ppufile);
  1116. paranr:=ppufile.getword;
  1117. { The var state of parameter symbols is fixed after writing them so
  1118. we write them to the unit file.
  1119. This enables constant folding for inline procedures loaded from units
  1120. }
  1121. varstate:=tvarstate(ppufile.getbyte);
  1122. paraloc[calleeside].init;
  1123. paraloc[callerside].init;
  1124. if vo_has_explicit_paraloc in varoptions then
  1125. begin
  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].location^));
  1152. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1153. end;
  1154. ppufile.writeentry(ibparavarsym);
  1155. end;
  1156. {****************************************************************************
  1157. TABSOLUTEVARSYM
  1158. ****************************************************************************}
  1159. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1160. begin
  1161. inherited create(absolutevarsym,n,vs_value,def,[]);
  1162. ref:=nil;
  1163. end;
  1164. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1165. begin
  1166. inherited create(absolutevarsym,n,vs_value,def,[]);
  1167. ref:=_ref;
  1168. end;
  1169. destructor tabsolutevarsym.destroy;
  1170. begin
  1171. if assigned(ref) then
  1172. ref.free;
  1173. inherited destroy;
  1174. end;
  1175. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1176. begin
  1177. inherited ppuload(absolutevarsym,ppufile);
  1178. ref:=nil;
  1179. asmname:=nil;
  1180. abstyp:=absolutetyp(ppufile.getbyte);
  1181. {$ifdef i386}
  1182. absseg:=false;
  1183. {$endif i386}
  1184. case abstyp of
  1185. tovar :
  1186. ref:=ppufile.getpropaccesslist;
  1187. toasm :
  1188. asmname:=stringdup(ppufile.getstring);
  1189. toaddr :
  1190. begin
  1191. addroffset:=ppufile.getaint;
  1192. {$ifdef i386}
  1193. absseg:=boolean(ppufile.getbyte);
  1194. {$endif i386}
  1195. end;
  1196. end;
  1197. end;
  1198. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1199. begin
  1200. inherited ppuwrite(ppufile);
  1201. ppufile.putbyte(byte(abstyp));
  1202. case abstyp of
  1203. tovar :
  1204. ppufile.putpropaccesslist(ref);
  1205. toasm :
  1206. ppufile.putstring(asmname^);
  1207. toaddr :
  1208. begin
  1209. ppufile.putaint(addroffset);
  1210. {$ifdef i386}
  1211. ppufile.putbyte(byte(absseg));
  1212. {$endif i386}
  1213. end;
  1214. end;
  1215. ppufile.writeentry(ibabsolutevarsym);
  1216. end;
  1217. procedure tabsolutevarsym.buildderef;
  1218. begin
  1219. inherited buildderef;
  1220. if (abstyp=tovar) then
  1221. ref.buildderef;
  1222. end;
  1223. procedure tabsolutevarsym.deref;
  1224. begin
  1225. inherited deref;
  1226. { own absolute deref }
  1227. if (abstyp=tovar) then
  1228. ref.resolve;
  1229. end;
  1230. function tabsolutevarsym.mangledname : string;
  1231. begin
  1232. case abstyp of
  1233. toasm :
  1234. mangledname:=asmname^;
  1235. toaddr :
  1236. mangledname:='$'+tostr(addroffset);
  1237. else
  1238. internalerror(200411062);
  1239. end;
  1240. end;
  1241. {****************************************************************************
  1242. TCONSTSYM
  1243. ****************************************************************************}
  1244. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1245. begin
  1246. inherited create(constsym,n);
  1247. fillchar(value, sizeof(value), #0);
  1248. consttyp:=t;
  1249. value.valueord:=v;
  1250. constdef:=def;
  1251. end;
  1252. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1253. begin
  1254. inherited create(constsym,n);
  1255. fillchar(value, sizeof(value), #0);
  1256. consttyp:=t;
  1257. value.valueordptr:=v;
  1258. constdef:=def;
  1259. end;
  1260. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1261. begin
  1262. inherited create(constsym,n);
  1263. fillchar(value, sizeof(value), #0);
  1264. consttyp:=t;
  1265. value.valueptr:=v;
  1266. constdef:=def;
  1267. end;
  1268. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1269. begin
  1270. inherited create(constsym,n);
  1271. fillchar(value, sizeof(value), #0);
  1272. consttyp:=t;
  1273. value.valueptr:=str;
  1274. constdef:=nil;
  1275. value.len:=l;
  1276. end;
  1277. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1278. begin
  1279. inherited create(constsym,n);
  1280. fillchar(value, sizeof(value), #0);
  1281. consttyp:=t;
  1282. pcompilerwidestring(value.valueptr):=pw;
  1283. constdef:=nil;
  1284. value.len:=getlengthwidestring(pw);
  1285. end;
  1286. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1287. var
  1288. pd : pbestreal;
  1289. ps : pnormalset;
  1290. pc : pchar;
  1291. pw : pcompilerwidestring;
  1292. begin
  1293. inherited ppuload(constsym,ppufile);
  1294. constdef:=nil;
  1295. consttyp:=tconsttyp(ppufile.getbyte);
  1296. fillchar(value, sizeof(value), #0);
  1297. case consttyp of
  1298. constord :
  1299. begin
  1300. ppufile.getderef(constdefderef);
  1301. value.valueord:=ppufile.getexprint;
  1302. end;
  1303. constpointer :
  1304. begin
  1305. ppufile.getderef(constdefderef);
  1306. value.valueordptr:=ppufile.getptruint;
  1307. end;
  1308. constwstring :
  1309. begin
  1310. initwidestring(pw);
  1311. setlengthwidestring(pw,ppufile.getlongint);
  1312. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  1313. pcompilerwidestring(value.valueptr):=pw;
  1314. end;
  1315. conststring,
  1316. constresourcestring :
  1317. begin
  1318. value.len:=ppufile.getlongint;
  1319. getmem(pc,value.len+1);
  1320. ppufile.getdata(pc^,value.len);
  1321. value.valueptr:=pc;
  1322. end;
  1323. constreal :
  1324. begin
  1325. new(pd);
  1326. pd^:=ppufile.getreal;
  1327. value.valueptr:=pd;
  1328. end;
  1329. constset :
  1330. begin
  1331. ppufile.getderef(constdefderef);
  1332. new(ps);
  1333. ppufile.getnormalset(ps^);
  1334. value.valueptr:=ps;
  1335. end;
  1336. constguid :
  1337. begin
  1338. new(pguid(value.valueptr));
  1339. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1340. end;
  1341. constnil : ;
  1342. else
  1343. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1344. end;
  1345. end;
  1346. destructor tconstsym.destroy;
  1347. begin
  1348. case consttyp of
  1349. conststring,
  1350. constresourcestring :
  1351. freemem(pchar(value.valueptr),value.len+1);
  1352. constwstring :
  1353. donewidestring(pcompilerwidestring(value.valueptr));
  1354. constreal :
  1355. dispose(pbestreal(value.valueptr));
  1356. constset :
  1357. dispose(pnormalset(value.valueptr));
  1358. constguid :
  1359. dispose(pguid(value.valueptr));
  1360. end;
  1361. inherited destroy;
  1362. end;
  1363. procedure tconstsym.buildderef;
  1364. begin
  1365. if consttyp in [constord,constpointer,constset] then
  1366. constdefderef.build(constdef);
  1367. end;
  1368. procedure tconstsym.deref;
  1369. begin
  1370. if consttyp in [constord,constpointer,constset] then
  1371. constdef:=tdef(constdefderef.resolve);
  1372. end;
  1373. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1374. begin
  1375. inherited ppuwrite(ppufile);
  1376. ppufile.putbyte(byte(consttyp));
  1377. case consttyp of
  1378. constnil : ;
  1379. constord :
  1380. begin
  1381. ppufile.putderef(constdefderef);
  1382. ppufile.putexprint(value.valueord);
  1383. end;
  1384. constpointer :
  1385. begin
  1386. ppufile.putderef(constdefderef);
  1387. ppufile.putptruint(value.valueordptr);
  1388. end;
  1389. constwstring :
  1390. begin
  1391. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1392. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1393. end;
  1394. conststring,
  1395. constresourcestring :
  1396. begin
  1397. ppufile.putlongint(value.len);
  1398. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1399. end;
  1400. constreal :
  1401. ppufile.putreal(pbestreal(value.valueptr)^);
  1402. constset :
  1403. begin
  1404. ppufile.putderef(constdefderef);
  1405. ppufile.putnormalset(value.valueptr^);
  1406. end;
  1407. constguid :
  1408. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1409. else
  1410. internalerror(13);
  1411. end;
  1412. ppufile.writeentry(ibconstsym);
  1413. end;
  1414. {****************************************************************************
  1415. TENUMSYM
  1416. ****************************************************************************}
  1417. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1418. begin
  1419. inherited create(enumsym,n);
  1420. definition:=def;
  1421. value:=v;
  1422. { First entry? Then we need to set the minval }
  1423. if def.firstenum=nil then
  1424. begin
  1425. if v>0 then
  1426. def.has_jumps:=true;
  1427. def.setmin(v);
  1428. def.setmax(v);
  1429. end
  1430. else
  1431. begin
  1432. { check for jumps }
  1433. if v>def.max+1 then
  1434. def.has_jumps:=true;
  1435. { update low and high }
  1436. if def.min>v then
  1437. def.setmin(v);
  1438. if def.max<v then
  1439. def.setmax(v);
  1440. end;
  1441. order;
  1442. end;
  1443. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1444. begin
  1445. inherited ppuload(enumsym,ppufile);
  1446. ppufile.getderef(definitionderef);
  1447. value:=ppufile.getlongint;
  1448. nextenum := Nil;
  1449. end;
  1450. procedure tenumsym.buildderef;
  1451. begin
  1452. definitionderef.build(definition);
  1453. end;
  1454. procedure tenumsym.deref;
  1455. begin
  1456. definition:=tenumdef(definitionderef.resolve);
  1457. order;
  1458. end;
  1459. procedure tenumsym.order;
  1460. var
  1461. sym : tenumsym;
  1462. begin
  1463. sym := tenumsym(definition.firstenum);
  1464. if sym = nil then
  1465. begin
  1466. definition.firstenum := self;
  1467. nextenum := nil;
  1468. exit;
  1469. end;
  1470. { reorder the symbols in increasing value }
  1471. if value < sym.value then
  1472. begin
  1473. nextenum := sym;
  1474. definition.firstenum := self;
  1475. end
  1476. else
  1477. begin
  1478. while (sym.value <= value) and assigned(sym.nextenum) do
  1479. sym := sym.nextenum;
  1480. nextenum := sym.nextenum;
  1481. sym.nextenum := self;
  1482. end;
  1483. end;
  1484. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1485. begin
  1486. inherited ppuwrite(ppufile);
  1487. ppufile.putderef(definitionderef);
  1488. ppufile.putlongint(value);
  1489. ppufile.writeentry(ibenumsym);
  1490. end;
  1491. {****************************************************************************
  1492. TTYPESYM
  1493. ****************************************************************************}
  1494. constructor ttypesym.create(const n : string;def:tdef);
  1495. begin
  1496. inherited create(typesym,n);
  1497. typedef:=def;
  1498. { register the typesym for the definition }
  1499. if assigned(typedef) and
  1500. (typedef.typ<>errordef) and
  1501. not(assigned(typedef.typesym)) then
  1502. typedef.typesym:=self;
  1503. end;
  1504. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1505. begin
  1506. inherited ppuload(typesym,ppufile);
  1507. ppufile.getderef(typedefderef);
  1508. end;
  1509. procedure ttypesym.buildderef;
  1510. begin
  1511. typedefderef.build(typedef);
  1512. end;
  1513. procedure ttypesym.deref;
  1514. begin
  1515. typedef:=tdef(typedefderef.resolve);
  1516. end;
  1517. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1518. begin
  1519. inherited ppuwrite(ppufile);
  1520. ppufile.putderef(typedefderef);
  1521. ppufile.writeentry(ibtypesym);
  1522. end;
  1523. {****************************************************************************
  1524. TSYSSYM
  1525. ****************************************************************************}
  1526. constructor tsyssym.create(const n : string;l : longint);
  1527. begin
  1528. inherited create(syssym,n);
  1529. number:=l;
  1530. end;
  1531. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1532. begin
  1533. inherited ppuload(syssym,ppufile);
  1534. number:=ppufile.getlongint;
  1535. end;
  1536. destructor tsyssym.destroy;
  1537. begin
  1538. inherited destroy;
  1539. end;
  1540. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1541. begin
  1542. inherited ppuwrite(ppufile);
  1543. ppufile.putlongint(number);
  1544. ppufile.writeentry(ibsyssym);
  1545. end;
  1546. {*****************************************************************************
  1547. TMacro
  1548. *****************************************************************************}
  1549. constructor tmacro.create(const n : string);
  1550. begin
  1551. inherited create(macrosym,n);
  1552. owner:=nil;
  1553. defined:=false;
  1554. is_used:=false;
  1555. is_compiler_var:=false;
  1556. buftext:=nil;
  1557. buflen:=0;
  1558. end;
  1559. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1560. begin
  1561. inherited ppuload(macrosym,ppufile);
  1562. defined:=boolean(ppufile.getbyte);
  1563. is_compiler_var:=boolean(ppufile.getbyte);
  1564. is_used:=false;
  1565. buflen:= ppufile.getlongint;
  1566. if buflen > 0 then
  1567. begin
  1568. getmem(buftext, buflen);
  1569. ppufile.getdata(buftext^, buflen)
  1570. end
  1571. else
  1572. buftext:=nil;
  1573. end;
  1574. destructor tmacro.destroy;
  1575. begin
  1576. if assigned(buftext) then
  1577. freemem(buftext);
  1578. inherited destroy;
  1579. end;
  1580. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1581. begin
  1582. inherited ppuwrite(ppufile);
  1583. ppufile.putbyte(byte(defined));
  1584. ppufile.putbyte(byte(is_compiler_var));
  1585. ppufile.putlongint(buflen);
  1586. if buflen > 0 then
  1587. ppufile.putdata(buftext^,buflen);
  1588. ppufile.writeentry(ibmacrosym);
  1589. end;
  1590. function tmacro.GetCopy:tmacro;
  1591. var
  1592. p : tmacro;
  1593. begin
  1594. p:=tmacro.create(realname);
  1595. p.defined:=defined;
  1596. p.is_used:=is_used;
  1597. p.is_compiler_var:=is_compiler_var;
  1598. p.buflen:=buflen;
  1599. if assigned(buftext) then
  1600. begin
  1601. getmem(p.buftext,buflen);
  1602. move(buftext^,p.buftext^,buflen);
  1603. end;
  1604. Result:=p;
  1605. end;
  1606. end.