ncon.pas 31 KB

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