ncon.pas 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for constants
  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 ncon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,widestr,constexp,
  22. cclasses,
  23. node,
  24. aasmbase,aasmtai,aasmdata,cpuinfo,globals,
  25. symconst,symtype,symdef,symsym;
  26. type
  27. tdataconstnode = class(tnode)
  28. data : tdynamicarray;
  29. maxalign : word;
  30. constructor create;virtual;
  31. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  32. destructor destroy;override;
  33. procedure ppuwrite(ppufile:tcompilerppufile);override;
  34. function dogetcopy : tnode;override;
  35. function pass_1 : tnode;override;
  36. function pass_typecheck:tnode;override;
  37. function docompare(p: tnode) : boolean; override;
  38. procedure printnodedata(var t:text);override;
  39. procedure append(const d;len : aint);
  40. procedure align(value : word);
  41. end;
  42. tdataconstnodeclass = class of tdataconstnode;
  43. trealconstnode = class(tnode)
  44. typedef : tdef;
  45. typedefderef : tderef;
  46. value_real : bestreal;
  47. value_currency : currency;
  48. lab_real : tasmlabel;
  49. constructor create(v : bestreal;def:tdef);virtual;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure buildderefimpl;override;
  53. procedure derefimpl;override;
  54. function dogetcopy : tnode;override;
  55. function pass_1 : tnode;override;
  56. function pass_typecheck:tnode;override;
  57. function docompare(p: tnode) : boolean; override;
  58. procedure printnodedata(var t:text);override;
  59. end;
  60. trealconstnodeclass = class of trealconstnode;
  61. tordconstnode = class(tnode)
  62. typedef : tdef;
  63. typedefderef : tderef;
  64. value : TConstExprInt;
  65. rangecheck : boolean;
  66. { create an ordinal constant node of the specified type and value.
  67. _rangecheck determines if the value of the ordinal should be checked
  68. against the ranges of the type definition.
  69. }
  70. constructor create(v : tconstexprint;def:tdef; _rangecheck : boolean);virtual;
  71. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  72. procedure ppuwrite(ppufile:tcompilerppufile);override;
  73. procedure buildderefimpl;override;
  74. procedure derefimpl;override;
  75. function dogetcopy : tnode;override;
  76. function pass_1 : tnode;override;
  77. function pass_typecheck:tnode;override;
  78. function docompare(p: tnode) : boolean; override;
  79. procedure printnodedata(var t:text);override;
  80. end;
  81. tordconstnodeclass = class of tordconstnode;
  82. tpointerconstnode = class(tnode)
  83. typedef : tdef;
  84. typedefderef : tderef;
  85. value : TConstPtrUInt;
  86. constructor create(v : TConstPtrUInt;def:tdef);virtual;
  87. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderefimpl;override;
  90. procedure derefimpl;override;
  91. function dogetcopy : tnode;override;
  92. function pass_1 : tnode;override;
  93. function pass_typecheck:tnode;override;
  94. function docompare(p: tnode) : boolean; override;
  95. end;
  96. tpointerconstnodeclass = class of tpointerconstnode;
  97. tconststringtype = (
  98. cst_conststring,
  99. cst_shortstring,
  100. cst_longstring,
  101. cst_ansistring,
  102. cst_widestring,
  103. cst_unicodestring
  104. );
  105. tstringconstnode = class(tnode)
  106. value_str : pchar;
  107. len : longint;
  108. lab_str : tasmlabel;
  109. cst_type : tconststringtype;
  110. constructor createstr(const s : string);virtual;
  111. constructor createpchar(s : pchar;l : longint);virtual;
  112. constructor createwstr(w : pcompilerwidestring);virtual;
  113. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  114. procedure ppuwrite(ppufile:tcompilerppufile);override;
  115. procedure buildderefimpl;override;
  116. procedure derefimpl;override;
  117. destructor destroy;override;
  118. function dogetcopy : tnode;override;
  119. function pass_1 : tnode;override;
  120. function pass_typecheck:tnode;override;
  121. function getpcharcopy : pchar;
  122. function docompare(p: tnode) : boolean; override;
  123. procedure changestringtype(def:tdef);
  124. function fullcompare(p: tstringconstnode): longint;
  125. end;
  126. tstringconstnodeclass = class of tstringconstnode;
  127. tsetconstnode = class(tunarynode)
  128. typedef : tdef;
  129. typedefderef : tderef;
  130. value_set : pconstset;
  131. lab_set : tasmlabel;
  132. constructor create(s : pconstset;def:tdef);virtual;
  133. destructor destroy;override;
  134. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  135. procedure ppuwrite(ppufile:tcompilerppufile);override;
  136. procedure buildderefimpl;override;
  137. procedure derefimpl;override;
  138. procedure adjustforsetbase;
  139. function dogetcopy : tnode;override;
  140. function pass_1 : tnode;override;
  141. function pass_typecheck:tnode;override;
  142. function docompare(p: tnode) : boolean; override;
  143. end;
  144. tsetconstnodeclass = class of tsetconstnode;
  145. tnilnode = class(tnode)
  146. constructor create;virtual;
  147. function pass_1 : tnode;override;
  148. function pass_typecheck:tnode;override;
  149. end;
  150. tnilnodeclass = class of tnilnode;
  151. tguidconstnode = class(tnode)
  152. value : tguid;
  153. constructor create(const g:tguid);virtual;
  154. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  155. procedure ppuwrite(ppufile:tcompilerppufile);override;
  156. function dogetcopy : tnode;override;
  157. function pass_1 : tnode;override;
  158. function pass_typecheck:tnode;override;
  159. function docompare(p: tnode) : boolean; override;
  160. end;
  161. tguidconstnodeclass = class of tguidconstnode;
  162. var
  163. cdataconstnode : tdataconstnodeclass;
  164. crealconstnode : trealconstnodeclass;
  165. cordconstnode : tordconstnodeclass;
  166. cpointerconstnode : tpointerconstnodeclass;
  167. cstringconstnode : tstringconstnodeclass;
  168. csetconstnode : tsetconstnodeclass;
  169. cguidconstnode : tguidconstnodeclass;
  170. cnilnode : tnilnodeclass;
  171. function genintconstnode(v : TConstExprInt) : tordconstnode;
  172. function genenumnode(v : tenumsym) : tordconstnode;
  173. { some helper routines }
  174. function get_ordinal_value(p : tnode) : TConstExprInt;
  175. function get_string_value(p : tnode; def: tstringdef) : tstringconstnode;
  176. function is_constresourcestringnode(p : tnode) : boolean;
  177. function is_emptyset(p : tnode):boolean;
  178. function genconstsymtree(p : tconstsym) : tnode;
  179. implementation
  180. uses
  181. cutils,
  182. verbose,systems,sysutils,
  183. defutil,
  184. cpubase,cgbase,
  185. nld;
  186. function genintconstnode(v : TConstExprInt) : tordconstnode;
  187. var
  188. htype : tdef;
  189. begin
  190. int_to_type(v,htype);
  191. genintconstnode:=cordconstnode.create(v,htype,true);
  192. end;
  193. function genenumnode(v : tenumsym) : tordconstnode;
  194. var
  195. htype : tdef;
  196. begin
  197. htype:=v.definition;
  198. genenumnode:=cordconstnode.create(int64(v.value),htype,true);
  199. end;
  200. function get_ordinal_value(p : tnode) : TConstExprInt;
  201. begin
  202. get_ordinal_value:=0;
  203. if is_constnode(p) then
  204. begin
  205. if p.nodetype=ordconstn then
  206. get_ordinal_value:=tordconstnode(p).value
  207. else
  208. Message(type_e_ordinal_expr_expected);
  209. end
  210. else
  211. Message(type_e_constant_expr_expected);
  212. end;
  213. function get_string_value(p: tnode; def: tstringdef): tstringconstnode;
  214. var
  215. stringVal: string;
  216. pWideStringVal: pcompilerwidestring;
  217. begin
  218. if is_constcharnode(p) then
  219. begin
  220. SetLength(stringVal,1);
  221. stringVal[1]:=char(tordconstnode(p).value.uvalue);
  222. result:=cstringconstnode.createstr(stringVal);
  223. end
  224. else if is_constwidecharnode(p) then
  225. begin
  226. initwidestring(pWideStringVal);
  227. concatwidestringchar(pWideStringVal, tcompilerwidechar(tordconstnode(p).value.uvalue));
  228. result:=cstringconstnode.createwstr(pWideStringVal);
  229. end
  230. else if is_conststringnode(p) then
  231. result:=tstringconstnode(p.getcopy)
  232. else
  233. begin
  234. Message(type_e_string_expr_expected);
  235. stringVal:='';
  236. result:=cstringconstnode.createstr(stringVal);
  237. end;
  238. result.changestringtype(def);
  239. end;
  240. function is_constresourcestringnode(p : tnode) : boolean;
  241. begin
  242. is_constresourcestringnode:=(p.nodetype=loadn) and
  243. (tloadnode(p).symtableentry.typ=constsym) and
  244. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  245. end;
  246. function is_emptyset(p : tnode):boolean;
  247. begin
  248. is_emptyset:=(p.nodetype=setconstn) and
  249. (Tsetconstnode(p).value_set^=[]);
  250. end;
  251. function genconstsymtree(p : tconstsym) : tnode;
  252. var
  253. p1 : tnode;
  254. len : longint;
  255. pc : pchar;
  256. begin
  257. p1:=nil;
  258. case p.consttyp of
  259. constord :
  260. p1:=cordconstnode.create(p.value.valueord,p.constdef,true);
  261. conststring :
  262. begin
  263. len:=p.value.len;
  264. getmem(pc,len+1);
  265. move(pchar(p.value.valueptr)^,pc^,len);
  266. pc[len]:=#0;
  267. p1:=cstringconstnode.createpchar(pc,len);
  268. end;
  269. constreal :
  270. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
  271. constset :
  272. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.constdef);
  273. constpointer :
  274. p1:=cpointerconstnode.create(p.value.valueordptr,p.constdef);
  275. constnil :
  276. p1:=cnilnode.create;
  277. else
  278. internalerror(200205103);
  279. end;
  280. genconstsymtree:=p1;
  281. end;
  282. {*****************************************************************************
  283. TDATACONSTNODE
  284. *****************************************************************************}
  285. constructor tdataconstnode.create;
  286. begin
  287. inherited create(dataconstn);
  288. data:=tdynamicarray.create(128);
  289. end;
  290. constructor tdataconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  291. var
  292. len : aint;
  293. buf : array[0..255] of byte;
  294. begin
  295. inherited ppuload(t,ppufile);
  296. len:=ppufile.getaint;
  297. if len<4096 then
  298. data:=tdynamicarray.create(len)
  299. else
  300. data:=tdynamicarray.create(4096);
  301. while len>0 do
  302. begin
  303. if len>sizeof(buf) then
  304. begin
  305. ppufile.getdata(buf,sizeof(buf));
  306. data.write(buf,sizeof(buf));
  307. dec(len,sizeof(buf));
  308. end
  309. else
  310. begin
  311. ppufile.getdata(buf,len);
  312. data.write(buf,len);
  313. len:=0;
  314. end;
  315. end;
  316. end;
  317. destructor tdataconstnode.destroy;
  318. begin
  319. data.free;
  320. inherited destroy;
  321. end;
  322. procedure tdataconstnode.ppuwrite(ppufile:tcompilerppufile);
  323. var
  324. len : aint;
  325. buf : array[0..255] of byte;
  326. begin
  327. inherited ppuwrite(ppufile);
  328. len:=data.size;
  329. ppufile.putaint(len);
  330. data.seek(0);
  331. while len>0 do
  332. begin
  333. if len>sizeof(buf) then
  334. begin
  335. data.read(buf,sizeof(buf));
  336. ppufile.putdata(buf,sizeof(buf));
  337. dec(len,sizeof(buf));
  338. end
  339. else
  340. begin
  341. data.read(buf,len);
  342. ppufile.putdata(buf,len);
  343. len:=0;
  344. end;
  345. end;
  346. end;
  347. function tdataconstnode.dogetcopy : tnode;
  348. var
  349. n : tdataconstnode;
  350. len : aint;
  351. buf : array[0..255] of byte;
  352. begin
  353. n:=tdataconstnode(inherited dogetcopy);
  354. len:=data.size;
  355. if len<4096 then
  356. n.data:=tdynamicarray.create(len)
  357. else
  358. n.data:=tdynamicarray.create(4096);
  359. data.seek(0);
  360. while len>0 do
  361. begin
  362. if len>sizeof(buf) then
  363. begin
  364. data.read(buf,sizeof(buf));
  365. n.data.write(buf,sizeof(buf));
  366. dec(len,sizeof(buf));
  367. end
  368. else
  369. begin
  370. data.read(buf,len);
  371. n.data.write(buf,len);
  372. len:=0;
  373. end;
  374. end;
  375. dogetcopy := n;
  376. end;
  377. function tdataconstnode.pass_1 : tnode;
  378. begin
  379. result:=nil;
  380. expectloc:=LOC_CREFERENCE;
  381. end;
  382. function tdataconstnode.pass_typecheck:tnode;
  383. begin
  384. result:=nil;
  385. resultdef:=voidpointertype;
  386. end;
  387. function tdataconstnode.docompare(p: tnode) : boolean;
  388. var
  389. b1,b2 : byte;
  390. I : longint;
  391. begin
  392. docompare :=
  393. inherited docompare(p) and (data.size=tdataconstnode(p).data.size);
  394. if docompare then
  395. begin
  396. data.seek(0);
  397. tdataconstnode(p).data.seek(0);
  398. for i:=0 to data.size-1 do
  399. begin
  400. data.read(b1,1);
  401. tdataconstnode(p).data.read(b2,1);
  402. if b1<>b2 then
  403. begin
  404. docompare:=false;
  405. exit;
  406. end;
  407. end;
  408. end;
  409. end;
  410. procedure tdataconstnode.printnodedata(var t:text);
  411. var
  412. i : longint;
  413. b : byte;
  414. begin
  415. inherited printnodedata(t);
  416. write(t,printnodeindention,'data size = ',data.size,' data = ');
  417. data.seek(0);
  418. for i:=0 to data.size-1 do
  419. begin
  420. data.read(b,1);
  421. if i=data.size-1 then
  422. writeln(t,b)
  423. else
  424. write(t,b,',');
  425. end;
  426. end;
  427. procedure tdataconstnode.append(const d;len : aint);
  428. begin
  429. data.seek(data.size);
  430. data.write(d,len);
  431. end;
  432. procedure tdataconstnode.align(value : word);
  433. begin
  434. if value>maxalign then
  435. maxalign:=value;
  436. data.align(value);
  437. end;
  438. {*****************************************************************************
  439. TREALCONSTNODE
  440. *****************************************************************************}
  441. { generic code }
  442. { overridden by: }
  443. { i386 }
  444. constructor trealconstnode.create(v : bestreal;def:tdef);
  445. begin
  446. if current_settings.fputype=fpu_none then
  447. internalerror(2008022401);
  448. inherited create(realconstn);
  449. typedef:=def;
  450. value_real:=v;
  451. value_currency:=v;
  452. lab_real:=nil;
  453. end;
  454. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  455. var
  456. i : int64;
  457. begin
  458. inherited ppuload(t,ppufile);
  459. ppufile.getderef(typedefderef);
  460. value_real:=ppufile.getreal;
  461. i:=ppufile.getint64;
  462. value_currency:=PCurrency(@i)^;
  463. lab_real:=tasmlabel(ppufile.getasmsymbol);
  464. end;
  465. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  466. begin
  467. inherited ppuwrite(ppufile);
  468. ppufile.putderef(typedefderef);
  469. ppufile.putreal(value_real);
  470. ppufile.putint64(PInt64(@value_currency)^);
  471. ppufile.putasmsymbol(lab_real);
  472. end;
  473. procedure trealconstnode.buildderefimpl;
  474. begin
  475. inherited buildderefimpl;
  476. typedefderef.build(typedef);
  477. end;
  478. procedure trealconstnode.derefimpl;
  479. begin
  480. inherited derefimpl;
  481. typedef:=tdef(typedefderef.resolve);
  482. end;
  483. function trealconstnode.dogetcopy : tnode;
  484. var
  485. n : trealconstnode;
  486. begin
  487. n:=trealconstnode(inherited dogetcopy);
  488. n.value_real:=value_real;
  489. n.value_currency:=value_currency;
  490. n.lab_real:=lab_real;
  491. dogetcopy:=n;
  492. end;
  493. function trealconstnode.pass_typecheck:tnode;
  494. begin
  495. result:=nil;
  496. resultdef:=typedef;
  497. end;
  498. function trealconstnode.pass_1 : tnode;
  499. begin
  500. result:=nil;
  501. expectloc:=LOC_CREFERENCE;
  502. end;
  503. function trealconstnode.docompare(p: tnode): boolean;
  504. begin
  505. docompare :=
  506. inherited docompare(p) and
  507. (value_real = trealconstnode(p).value_real) and
  508. { floating point compares for non-numbers give strange results usually }
  509. is_number_float(value_real) and
  510. is_number_float(trealconstnode(p).value_real);
  511. end;
  512. procedure Trealconstnode.printnodedata(var t:text);
  513. begin
  514. inherited printnodedata(t);
  515. writeln(t,printnodeindention,'value = ',value_real);
  516. end;
  517. {*****************************************************************************
  518. TORDCONSTNODE
  519. *****************************************************************************}
  520. constructor tordconstnode.create(v : tconstexprint;def:tdef;_rangecheck : boolean);
  521. begin
  522. inherited create(ordconstn);
  523. value:=v;
  524. typedef:=def;
  525. rangecheck := _rangecheck;
  526. end;
  527. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  528. begin
  529. inherited ppuload(t,ppufile);
  530. ppufile.getderef(typedefderef);
  531. value:=ppufile.getexprint;
  532. { normally, the value is already compiled, so we don't need
  533. to do once again a range check
  534. }
  535. rangecheck := false;
  536. end;
  537. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  538. begin
  539. inherited ppuwrite(ppufile);
  540. ppufile.putderef(typedefderef);
  541. ppufile.putexprint(value);
  542. end;
  543. procedure tordconstnode.buildderefimpl;
  544. begin
  545. inherited buildderefimpl;
  546. typedefderef.build(typedef);
  547. end;
  548. procedure tordconstnode.derefimpl;
  549. begin
  550. inherited derefimpl;
  551. typedef:=tdef(typedefderef.resolve);
  552. end;
  553. function tordconstnode.dogetcopy : tnode;
  554. var
  555. n : tordconstnode;
  556. begin
  557. n:=tordconstnode(inherited dogetcopy);
  558. n.value:=value;
  559. n.typedef := typedef;
  560. dogetcopy:=n;
  561. end;
  562. function tordconstnode.pass_typecheck:tnode;
  563. begin
  564. result:=nil;
  565. resultdef:=typedef;
  566. { only do range checking when explicitly asked for it
  567. and if the type can be range checked, see tests/tbs/tb0539.pp }
  568. if (resultdef.typ in [orddef,enumdef]) then
  569. testrange(resultdef,value,not rangecheck)
  570. end;
  571. function tordconstnode.pass_1 : tnode;
  572. begin
  573. result:=nil;
  574. expectloc:=LOC_CONSTANT;
  575. end;
  576. function tordconstnode.docompare(p: tnode): boolean;
  577. begin
  578. docompare :=
  579. inherited docompare(p) and
  580. (value = tordconstnode(p).value);
  581. end;
  582. procedure Tordconstnode.printnodedata(var t:text);
  583. begin
  584. inherited printnodedata(t);
  585. writeln(t,printnodeindention,'value = ',tostr(value));
  586. end;
  587. {*****************************************************************************
  588. TPOINTERCONSTNODE
  589. *****************************************************************************}
  590. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  591. begin
  592. inherited create(pointerconstn);
  593. value:=v;
  594. typedef:=def;
  595. end;
  596. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  597. begin
  598. inherited ppuload(t,ppufile);
  599. ppufile.getderef(typedefderef);
  600. value:=ppufile.getptruint;
  601. end;
  602. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  603. begin
  604. inherited ppuwrite(ppufile);
  605. ppufile.putderef(typedefderef);
  606. ppufile.putptruint(value);
  607. end;
  608. procedure tpointerconstnode.buildderefimpl;
  609. begin
  610. inherited buildderefimpl;
  611. typedefderef.build(typedef);
  612. end;
  613. procedure tpointerconstnode.derefimpl;
  614. begin
  615. inherited derefimpl;
  616. typedef:=tdef(typedefderef.resolve);
  617. end;
  618. function tpointerconstnode.dogetcopy : tnode;
  619. var
  620. n : tpointerconstnode;
  621. begin
  622. n:=tpointerconstnode(inherited dogetcopy);
  623. n.value:=value;
  624. n.typedef := typedef;
  625. dogetcopy:=n;
  626. end;
  627. function tpointerconstnode.pass_typecheck:tnode;
  628. begin
  629. result:=nil;
  630. resultdef:=typedef;
  631. end;
  632. function tpointerconstnode.pass_1 : tnode;
  633. begin
  634. result:=nil;
  635. expectloc:=LOC_CONSTANT;
  636. end;
  637. function tpointerconstnode.docompare(p: tnode): boolean;
  638. begin
  639. docompare :=
  640. inherited docompare(p) and
  641. (value = tpointerconstnode(p).value);
  642. end;
  643. {*****************************************************************************
  644. TSTRINGCONSTNODE
  645. *****************************************************************************}
  646. constructor tstringconstnode.createstr(const s : string);
  647. var
  648. l : longint;
  649. begin
  650. inherited create(stringconstn);
  651. l:=length(s);
  652. len:=l;
  653. { stringdup write even past a #0 }
  654. getmem(value_str,l+1);
  655. move(s[1],value_str^,l);
  656. value_str[l]:=#0;
  657. lab_str:=nil;
  658. cst_type:=cst_conststring;
  659. end;
  660. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  661. begin
  662. inherited create(stringconstn);
  663. len:=getlengthwidestring(w);
  664. initwidestring(pcompilerwidestring(value_str));
  665. copywidestring(w,pcompilerwidestring(value_str));
  666. lab_str:=nil;
  667. cst_type:=cst_widestring;
  668. end;
  669. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  670. begin
  671. inherited create(stringconstn);
  672. len:=l;
  673. value_str:=s;
  674. cst_type:=cst_conststring;
  675. lab_str:=nil;
  676. end;
  677. destructor tstringconstnode.destroy;
  678. begin
  679. if cst_type in [cst_widestring,cst_unicodestring] then
  680. donewidestring(pcompilerwidestring(value_str))
  681. else
  682. ansistringdispose(value_str,len);
  683. inherited destroy;
  684. end;
  685. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  686. var
  687. pw : pcompilerwidestring;
  688. begin
  689. inherited ppuload(t,ppufile);
  690. cst_type:=tconststringtype(ppufile.getbyte);
  691. len:=ppufile.getlongint;
  692. if cst_type in [cst_widestring,cst_unicodestring] then
  693. begin
  694. initwidestring(pw);
  695. setlengthwidestring(pw,len);
  696. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  697. pcompilerwidestring(value_str):=pw
  698. end
  699. else
  700. begin
  701. getmem(value_str,len+1);
  702. ppufile.getdata(value_str^,len);
  703. value_str[len]:=#0;
  704. end;
  705. lab_str:=tasmlabel(ppufile.getasmsymbol);
  706. end;
  707. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  708. begin
  709. inherited ppuwrite(ppufile);
  710. ppufile.putbyte(byte(cst_type));
  711. ppufile.putlongint(len);
  712. if cst_type in [cst_widestring,cst_unicodestring] then
  713. ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
  714. else
  715. ppufile.putdata(value_str^,len);
  716. ppufile.putasmsymbol(lab_str);
  717. end;
  718. procedure tstringconstnode.buildderefimpl;
  719. begin
  720. inherited buildderefimpl;
  721. end;
  722. procedure tstringconstnode.derefimpl;
  723. begin
  724. inherited derefimpl;
  725. end;
  726. function tstringconstnode.dogetcopy : tnode;
  727. var
  728. n : tstringconstnode;
  729. begin
  730. n:=tstringconstnode(inherited dogetcopy);
  731. n.cst_type:=cst_type;
  732. n.len:=len;
  733. n.lab_str:=lab_str;
  734. if cst_type in [cst_widestring,cst_unicodestring] then
  735. begin
  736. initwidestring(pcompilerwidestring(n.value_str));
  737. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  738. end
  739. else
  740. n.value_str:=getpcharcopy;
  741. dogetcopy:=n;
  742. end;
  743. function tstringconstnode.pass_typecheck:tnode;
  744. var
  745. l : aint;
  746. begin
  747. result:=nil;
  748. case cst_type of
  749. cst_conststring :
  750. begin
  751. { handle and store as array[0..len-1] of char }
  752. if len>0 then
  753. l:=len-1
  754. else
  755. l:=0;
  756. resultdef:=tarraydef.create(0,l,s32inttype);
  757. tarraydef(resultdef).elementdef:=cchartype;
  758. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  759. end;
  760. cst_shortstring :
  761. resultdef:=cshortstringtype;
  762. cst_ansistring :
  763. resultdef:=cansistringtype;
  764. cst_unicodestring :
  765. resultdef:=cunicodestringtype;
  766. cst_widestring :
  767. resultdef:=cwidestringtype;
  768. cst_longstring :
  769. resultdef:=clongstringtype;
  770. end;
  771. end;
  772. function tstringconstnode.pass_1 : tnode;
  773. begin
  774. result:=nil;
  775. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  776. begin
  777. if len=0 then
  778. expectloc:=LOC_CONSTANT
  779. else
  780. expectloc:=LOC_REGISTER
  781. end
  782. else
  783. expectloc:=LOC_CREFERENCE;
  784. end;
  785. function tstringconstnode.getpcharcopy : pchar;
  786. var
  787. pc : pchar;
  788. begin
  789. pc:=nil;
  790. getmem(pc,len+1);
  791. if pc=nil then
  792. Message(general_f_no_memory_left);
  793. move(value_str^,pc^,len+1);
  794. getpcharcopy:=pc;
  795. end;
  796. function tstringconstnode.docompare(p: tnode): boolean;
  797. begin
  798. docompare :=
  799. inherited docompare(p) and
  800. (len = tstringconstnode(p).len) and
  801. { Don't compare the pchars, since they may contain null chars }
  802. { Since all equal constant strings are replaced by the same }
  803. { label, the following compare should be enough (JM) }
  804. (lab_str = tstringconstnode(p).lab_str);
  805. end;
  806. procedure tstringconstnode.changestringtype(def:tdef);
  807. const
  808. st2cst : array[tstringtype] of tconststringtype = (
  809. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  810. var
  811. pw : pcompilerwidestring;
  812. pc : pchar;
  813. begin
  814. if def.typ<>stringdef then
  815. internalerror(200510011);
  816. { convert ascii 2 unicode }
  817. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  818. not(cst_type in [cst_widestring,cst_unicodestring]) then
  819. begin
  820. initwidestring(pw);
  821. ascii2unicode(value_str,len,pw);
  822. ansistringdispose(value_str,len);
  823. pcompilerwidestring(value_str):=pw;
  824. end
  825. else
  826. { convert unicode 2 ascii }
  827. if (cst_type in [cst_widestring,cst_unicodestring]) and
  828. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  829. begin
  830. pw:=pcompilerwidestring(value_str);
  831. getmem(pc,getlengthwidestring(pw)+1);
  832. unicode2ascii(pw,pc);
  833. donewidestring(pw);
  834. value_str:=pc;
  835. end;
  836. cst_type:=st2cst[tstringdef(def).stringtype];
  837. resultdef:=def;
  838. end;
  839. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  840. begin
  841. if cst_type<>p.cst_type then
  842. InternalError(2009121701);
  843. if cst_type in [cst_widestring,cst_unicodestring] then
  844. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  845. else
  846. result:=compareansistrings(value_str,p.value_str,len,p.len);
  847. end;
  848. {*****************************************************************************
  849. TSETCONSTNODE
  850. *****************************************************************************}
  851. constructor tsetconstnode.create(s : pconstset;def:tdef);
  852. begin
  853. inherited create(setconstn,nil);
  854. typedef:=def;
  855. if assigned(s) then
  856. begin
  857. new(value_set);
  858. value_set^:=s^;
  859. end
  860. else
  861. value_set:=nil;
  862. end;
  863. destructor tsetconstnode.destroy;
  864. begin
  865. if assigned(value_set) then
  866. dispose(value_set);
  867. inherited destroy;
  868. end;
  869. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  870. begin
  871. inherited ppuload(t,ppufile);
  872. ppufile.getderef(typedefderef);
  873. new(value_set);
  874. ppufile.getnormalset(value_set^);
  875. end;
  876. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  877. begin
  878. inherited ppuwrite(ppufile);
  879. ppufile.putderef(typedefderef);
  880. ppufile.putnormalset(value_set^);
  881. end;
  882. procedure tsetconstnode.buildderefimpl;
  883. begin
  884. inherited buildderefimpl;
  885. typedefderef.build(typedef);
  886. end;
  887. procedure tsetconstnode.derefimpl;
  888. begin
  889. inherited derefimpl;
  890. typedef:=tdef(typedefderef.resolve);
  891. end;
  892. procedure tsetconstnode.adjustforsetbase;
  893. type
  894. setbytes = array[0..31] of byte;
  895. Psetbytes = ^setbytes;
  896. var
  897. i, diff: longint;
  898. begin
  899. { Internally, the compiler stores all sets with setbase 0, so we have }
  900. { to convert the set to its actual format in case setbase<>0 when }
  901. { writing it out }
  902. if (tsetdef(resultdef).setbase<>0) then
  903. begin
  904. if (tsetdef(resultdef).setbase and 7)<>0 then
  905. internalerror(2007091501);
  906. diff:=tsetdef(resultdef).setbase div 8;
  907. { This is endian-neutral in the new set format: in both cases, }
  908. { the first byte contains the first elements of the set. }
  909. { Since the compiler/base rtl cannot contain packed sets before }
  910. { they work for big endian, it's no problem that the code below }
  911. { is wrong for the old big endian set format (setbase cannot be }
  912. { <>0 with non-packed sets). }
  913. for i:=0 to tsetdef(resultdef).size-1 do
  914. begin
  915. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  916. Psetbytes(value_set)^[i+diff]:=0;
  917. end;
  918. end;
  919. end;
  920. function tsetconstnode.dogetcopy : tnode;
  921. var
  922. n : tsetconstnode;
  923. begin
  924. n:=tsetconstnode(inherited dogetcopy);
  925. if assigned(value_set) then
  926. begin
  927. new(n.value_set);
  928. n.value_set^:=value_set^
  929. end
  930. else
  931. n.value_set:=nil;
  932. n.typedef := typedef;
  933. n.lab_set:=lab_set;
  934. dogetcopy:=n;
  935. end;
  936. function tsetconstnode.pass_typecheck:tnode;
  937. begin
  938. result:=nil;
  939. resultdef:=typedef;
  940. end;
  941. function tsetconstnode.pass_1 : tnode;
  942. begin
  943. result:=nil;
  944. if is_smallset(resultdef) then
  945. expectloc:=LOC_CONSTANT
  946. else
  947. expectloc:=LOC_CREFERENCE;
  948. end;
  949. function tsetconstnode.docompare(p: tnode): boolean;
  950. begin
  951. docompare:=(inherited docompare(p)) and
  952. (value_set^=Tsetconstnode(p).value_set^);
  953. end;
  954. {*****************************************************************************
  955. TNILNODE
  956. *****************************************************************************}
  957. constructor tnilnode.create;
  958. begin
  959. inherited create(niln);
  960. end;
  961. function tnilnode.pass_typecheck:tnode;
  962. begin
  963. result:=nil;
  964. resultdef:=voidpointertype;
  965. end;
  966. function tnilnode.pass_1 : tnode;
  967. begin
  968. result:=nil;
  969. expectloc:=LOC_CONSTANT;
  970. end;
  971. {*****************************************************************************
  972. TGUIDCONSTNODE
  973. *****************************************************************************}
  974. constructor tguidconstnode.create(const g:tguid);
  975. begin
  976. inherited create(guidconstn);
  977. value:=g;
  978. end;
  979. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  980. begin
  981. inherited ppuload(t,ppufile);
  982. ppufile.getguid(value);
  983. end;
  984. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  985. begin
  986. inherited ppuwrite(ppufile);
  987. ppufile.putguid(value);
  988. end;
  989. function tguidconstnode.dogetcopy : tnode;
  990. var
  991. n : tguidconstnode;
  992. begin
  993. n:=tguidconstnode(inherited dogetcopy);
  994. n.value:=value;
  995. dogetcopy:=n;
  996. end;
  997. function tguidconstnode.pass_typecheck:tnode;
  998. begin
  999. result:=nil;
  1000. resultdef:=rec_tguid;
  1001. end;
  1002. function tguidconstnode.pass_1 : tnode;
  1003. begin
  1004. result:=nil;
  1005. expectloc:=LOC_CREFERENCE;
  1006. end;
  1007. function tguidconstnode.docompare(p: tnode): boolean;
  1008. begin
  1009. docompare :=
  1010. inherited docompare(p) and
  1011. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1012. end;
  1013. begin
  1014. crealconstnode:=trealconstnode;
  1015. cordconstnode:=tordconstnode;
  1016. cpointerconstnode:=tpointerconstnode;
  1017. cstringconstnode:=tstringconstnode;
  1018. csetconstnode:=tsetconstnode;
  1019. cnilnode:=tnilnode;
  1020. cguidconstnode:=tguidconstnode;
  1021. end.