ncon.pas 33 KB

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