ncon.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. i : longint;
  660. begin
  661. inherited ppuload(t,ppufile);
  662. cst_type:=tconststringtype(ppufile.getbyte);
  663. len:=ppufile.getlongint;
  664. if cst_type in [cst_widestring,cst_unicodestring] then
  665. begin
  666. initwidestring(pw);
  667. setlengthwidestring(pw,len);
  668. { don't use getdata, because the compilerwidechars may have to
  669. be byteswapped
  670. }
  671. {$if sizeof(tcompilerwidechar) = 2}
  672. for i:=0 to pw^.len-1 do
  673. pw^.data[i]:=ppufile.getword;
  674. {$elseif sizeof(tcompilerwidechar) = 4}
  675. for i:=0 to pw^.len-1 do
  676. pw^.data[i]:=cardinal(ppufile.getlongint);
  677. {$else}
  678. {$error Unsupported tcompilerwidechar size}
  679. {$endif}
  680. pcompilerwidestring(value_str):=pw
  681. end
  682. else
  683. begin
  684. getmem(value_str,len+1);
  685. ppufile.getdata(value_str^,len);
  686. value_str[len]:=#0;
  687. end;
  688. lab_str:=tasmlabel(ppufile.getasmsymbol);
  689. end;
  690. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  691. begin
  692. inherited ppuwrite(ppufile);
  693. ppufile.putbyte(byte(cst_type));
  694. ppufile.putlongint(len);
  695. if cst_type in [cst_widestring,cst_unicodestring] then
  696. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  697. else
  698. ppufile.putdata(value_str^,len);
  699. ppufile.putasmsymbol(lab_str);
  700. end;
  701. procedure tstringconstnode.buildderefimpl;
  702. begin
  703. inherited buildderefimpl;
  704. end;
  705. procedure tstringconstnode.derefimpl;
  706. begin
  707. inherited derefimpl;
  708. end;
  709. function tstringconstnode.dogetcopy : tnode;
  710. var
  711. n : tstringconstnode;
  712. begin
  713. n:=tstringconstnode(inherited dogetcopy);
  714. n.cst_type:=cst_type;
  715. n.len:=len;
  716. n.lab_str:=lab_str;
  717. if cst_type in [cst_widestring,cst_unicodestring] then
  718. begin
  719. initwidestring(pcompilerwidestring(n.value_str));
  720. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  721. end
  722. else
  723. n.value_str:=getpcharcopy;
  724. dogetcopy:=n;
  725. end;
  726. function tstringconstnode.pass_typecheck:tnode;
  727. var
  728. l : aint;
  729. begin
  730. result:=nil;
  731. case cst_type of
  732. cst_conststring :
  733. begin
  734. { handle and store as array[0..len-1] of char }
  735. if len>0 then
  736. l:=len-1
  737. else
  738. l:=0;
  739. resultdef:=tarraydef.create(0,l,s32inttype);
  740. tarraydef(resultdef).elementdef:=cchartype;
  741. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  742. end;
  743. cst_shortstring :
  744. resultdef:=cshortstringtype;
  745. cst_ansistring :
  746. resultdef:=cansistringtype;
  747. cst_unicodestring :
  748. resultdef:=cunicodestringtype;
  749. cst_widestring :
  750. resultdef:=cwidestringtype;
  751. cst_longstring :
  752. resultdef:=clongstringtype;
  753. end;
  754. end;
  755. function tstringconstnode.pass_1 : tnode;
  756. begin
  757. result:=nil;
  758. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  759. begin
  760. if len=0 then
  761. expectloc:=LOC_CONSTANT
  762. else
  763. expectloc:=LOC_REGISTER
  764. end
  765. else
  766. expectloc:=LOC_CREFERENCE;
  767. end;
  768. function tstringconstnode.getpcharcopy : pchar;
  769. var
  770. pc : pchar;
  771. begin
  772. pc:=nil;
  773. getmem(pc,len+1);
  774. if pc=nil then
  775. Message(general_f_no_memory_left);
  776. move(value_str^,pc^,len+1);
  777. getpcharcopy:=pc;
  778. end;
  779. function tstringconstnode.docompare(p: tnode): boolean;
  780. begin
  781. docompare :=
  782. inherited docompare(p) and
  783. (len = tstringconstnode(p).len) and
  784. { Don't compare the pchars, since they may contain null chars }
  785. { Since all equal constant strings are replaced by the same }
  786. { label, the following compare should be enough (JM) }
  787. (lab_str = tstringconstnode(p).lab_str);
  788. end;
  789. procedure tstringconstnode.changestringtype(def:tdef);
  790. const
  791. st2cst : array[tstringtype] of tconststringtype = (
  792. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  793. var
  794. pw : pcompilerwidestring;
  795. pc : pchar;
  796. begin
  797. if def.typ<>stringdef then
  798. internalerror(200510011);
  799. { convert ascii 2 unicode }
  800. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  801. not(cst_type in [cst_widestring,cst_unicodestring]) then
  802. begin
  803. initwidestring(pw);
  804. ascii2unicode(value_str,len,pw);
  805. ansistringdispose(value_str,len);
  806. pcompilerwidestring(value_str):=pw;
  807. end
  808. else
  809. { convert unicode 2 ascii }
  810. if (cst_type in [cst_widestring,cst_unicodestring]) and
  811. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  812. begin
  813. pw:=pcompilerwidestring(value_str);
  814. getmem(pc,getlengthwidestring(pw)+1);
  815. unicode2ascii(pw,pc);
  816. donewidestring(pw);
  817. value_str:=pc;
  818. end;
  819. cst_type:=st2cst[tstringdef(def).stringtype];
  820. resultdef:=def;
  821. end;
  822. {*****************************************************************************
  823. TSETCONSTNODE
  824. *****************************************************************************}
  825. constructor tsetconstnode.create(s : pconstset;def:tdef);
  826. begin
  827. inherited create(setconstn,nil);
  828. typedef:=def;
  829. if assigned(s) then
  830. begin
  831. new(value_set);
  832. value_set^:=s^;
  833. end
  834. else
  835. value_set:=nil;
  836. end;
  837. destructor tsetconstnode.destroy;
  838. begin
  839. if assigned(value_set) then
  840. dispose(value_set);
  841. inherited destroy;
  842. end;
  843. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  844. begin
  845. inherited ppuload(t,ppufile);
  846. ppufile.getderef(typedefderef);
  847. new(value_set);
  848. ppufile.getnormalset(value_set^);
  849. end;
  850. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  851. begin
  852. inherited ppuwrite(ppufile);
  853. ppufile.putderef(typedefderef);
  854. ppufile.putnormalset(value_set^);
  855. end;
  856. procedure tsetconstnode.buildderefimpl;
  857. begin
  858. inherited buildderefimpl;
  859. typedefderef.build(typedef);
  860. end;
  861. procedure tsetconstnode.derefimpl;
  862. begin
  863. inherited derefimpl;
  864. typedef:=tdef(typedefderef.resolve);
  865. end;
  866. procedure tsetconstnode.adjustforsetbase;
  867. type
  868. setbytes = array[0..31] of byte;
  869. Psetbytes = ^setbytes;
  870. var
  871. i, diff: longint;
  872. begin
  873. { Internally, the compiler stores all sets with setbase 0, so we have }
  874. { to convert the set to its actual format in case setbase<>0 when }
  875. { writing it out }
  876. if (tsetdef(resultdef).setbase<>0) then
  877. begin
  878. if (tsetdef(resultdef).setbase and 7)<>0 then
  879. internalerror(2007091501);
  880. diff:=tsetdef(resultdef).setbase div 8;
  881. { This is endian-neutral in the new set format: in both cases, }
  882. { the first byte contains the first elements of the set. }
  883. { Since the compiler/base rtl cannot contain packed sets before }
  884. { they work for big endian, it's no problem that the code below }
  885. { is wrong for the old big endian set format (setbase cannot be }
  886. { <>0 with non-packed sets). }
  887. for i:=0 to tsetdef(resultdef).size-1 do
  888. begin
  889. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  890. Psetbytes(value_set)^[i+diff]:=0;
  891. end;
  892. end;
  893. end;
  894. function tsetconstnode.dogetcopy : tnode;
  895. var
  896. n : tsetconstnode;
  897. begin
  898. n:=tsetconstnode(inherited dogetcopy);
  899. if assigned(value_set) then
  900. begin
  901. new(n.value_set);
  902. n.value_set^:=value_set^
  903. end
  904. else
  905. n.value_set:=nil;
  906. n.typedef := typedef;
  907. n.lab_set:=lab_set;
  908. dogetcopy:=n;
  909. end;
  910. function tsetconstnode.pass_typecheck:tnode;
  911. begin
  912. result:=nil;
  913. resultdef:=typedef;
  914. end;
  915. function tsetconstnode.pass_1 : tnode;
  916. begin
  917. result:=nil;
  918. if is_smallset(resultdef) then
  919. expectloc:=LOC_CONSTANT
  920. else
  921. expectloc:=LOC_CREFERENCE;
  922. end;
  923. function tsetconstnode.docompare(p: tnode): boolean;
  924. begin
  925. docompare:=(inherited docompare(p)) and
  926. (value_set^=Tsetconstnode(p).value_set^);
  927. end;
  928. {*****************************************************************************
  929. TNILNODE
  930. *****************************************************************************}
  931. constructor tnilnode.create;
  932. begin
  933. inherited create(niln);
  934. end;
  935. function tnilnode.pass_typecheck:tnode;
  936. begin
  937. result:=nil;
  938. resultdef:=voidpointertype;
  939. end;
  940. function tnilnode.pass_1 : tnode;
  941. begin
  942. result:=nil;
  943. expectloc:=LOC_CONSTANT;
  944. end;
  945. {*****************************************************************************
  946. TGUIDCONSTNODE
  947. *****************************************************************************}
  948. constructor tguidconstnode.create(const g:tguid);
  949. begin
  950. inherited create(guidconstn);
  951. value:=g;
  952. end;
  953. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  954. begin
  955. inherited ppuload(t,ppufile);
  956. ppufile.getguid(value);
  957. end;
  958. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  959. begin
  960. inherited ppuwrite(ppufile);
  961. ppufile.putguid(value);
  962. end;
  963. function tguidconstnode.dogetcopy : tnode;
  964. var
  965. n : tguidconstnode;
  966. begin
  967. n:=tguidconstnode(inherited dogetcopy);
  968. n.value:=value;
  969. dogetcopy:=n;
  970. end;
  971. function tguidconstnode.pass_typecheck:tnode;
  972. begin
  973. result:=nil;
  974. resultdef:=rec_tguid;
  975. end;
  976. function tguidconstnode.pass_1 : tnode;
  977. begin
  978. result:=nil;
  979. expectloc:=LOC_CREFERENCE;
  980. end;
  981. function tguidconstnode.docompare(p: tnode): boolean;
  982. begin
  983. docompare :=
  984. inherited docompare(p) and
  985. (guid2string(value) = guid2string(tguidconstnode(p).value));
  986. end;
  987. begin
  988. crealconstnode:=trealconstnode;
  989. cordconstnode:=tordconstnode;
  990. cpointerconstnode:=tpointerconstnode;
  991. cstringconstnode:=tstringconstnode;
  992. csetconstnode:=tsetconstnode;
  993. cnilnode:=tnilnode;
  994. cguidconstnode:=tguidconstnode;
  995. end.