ncon.pas 31 KB

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