symsym.pas 59 KB

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