ncon.pas 33 KB

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