ncon.pas 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncon;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,widestr,
  23. node,
  24. aasmbase,aasmtai,cpuinfo,globals,
  25. symconst,symtype,symdef,symsym;
  26. type
  27. trealconstnode = class(tnode)
  28. restype : ttype;
  29. value_real : bestreal;
  30. lab_real : tasmlabel;
  31. constructor create(v : bestreal;const t:ttype);virtual;
  32. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  33. procedure ppuwrite(ppufile:tcompilerppufile);override;
  34. procedure buildderefimpl;override;
  35. procedure derefimpl;override;
  36. function getcopy : tnode;override;
  37. function pass_1 : tnode;override;
  38. function det_resulttype:tnode;override;
  39. function docompare(p: tnode) : boolean; override;
  40. procedure printnodedata(var t:text);override;
  41. end;
  42. trealconstnodeclass = class of trealconstnode;
  43. tordconstnode = class(tnode)
  44. restype : ttype;
  45. value : TConstExprInt;
  46. rangecheck : boolean;
  47. { create an ordinal constant node of the specified type and value.
  48. _rangecheck determines if the value of the ordinal should be checked
  49. against the ranges of the type definition.
  50. }
  51. constructor create(v : tconstexprint;const t:ttype; _rangecheck : boolean);virtual;
  52. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  53. procedure ppuwrite(ppufile:tcompilerppufile);override;
  54. procedure buildderefimpl;override;
  55. procedure derefimpl;override;
  56. function getcopy : tnode;override;
  57. function pass_1 : tnode;override;
  58. function det_resulttype:tnode;override;
  59. function docompare(p: tnode) : boolean; override;
  60. procedure printnodedata(var t:text);override;
  61. end;
  62. tordconstnodeclass = class of tordconstnode;
  63. tpointerconstnode = class(tnode)
  64. restype : ttype;
  65. value : TConstPtrUInt;
  66. constructor create(v : TConstPtrUInt;const t:ttype);virtual;
  67. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  68. procedure ppuwrite(ppufile:tcompilerppufile);override;
  69. procedure buildderefimpl;override;
  70. procedure derefimpl;override;
  71. function getcopy : tnode;override;
  72. function pass_1 : tnode;override;
  73. function det_resulttype:tnode;override;
  74. function docompare(p: tnode) : boolean; override;
  75. end;
  76. tpointerconstnodeclass = class of tpointerconstnode;
  77. tstringconstnode = class(tnode)
  78. value_str : pchar;
  79. len : aword;
  80. lab_str : tasmlabel;
  81. st_type : tstringtype;
  82. constructor createstr(const s : string;st:tstringtype);virtual;
  83. constructor createpchar(s : pchar;l : longint);virtual;
  84. constructor createwstr(w : pcompilerwidestring);virtual;
  85. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  86. procedure ppuwrite(ppufile:tcompilerppufile);override;
  87. procedure buildderefimpl;override;
  88. procedure derefimpl;override;
  89. destructor destroy;override;
  90. function getcopy : tnode;override;
  91. function pass_1 : tnode;override;
  92. function det_resulttype:tnode;override;
  93. function getpcharcopy : pchar;
  94. function docompare(p: tnode) : boolean; override;
  95. end;
  96. tstringconstnodeclass = class of tstringconstnode;
  97. tsetconstnode = class(tunarynode)
  98. restype : ttype;
  99. value_set : pconstset;
  100. lab_set : tasmlabel;
  101. constructor create(s : pconstset;const t:ttype);virtual;
  102. destructor destroy;override;
  103. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  104. procedure ppuwrite(ppufile:tcompilerppufile);override;
  105. procedure buildderefimpl;override;
  106. procedure derefimpl;override;
  107. function getcopy : tnode;override;
  108. function pass_1 : tnode;override;
  109. function det_resulttype:tnode;override;
  110. function docompare(p: tnode) : boolean; override;
  111. end;
  112. tsetconstnodeclass = class of tsetconstnode;
  113. tnilnode = class(tnode)
  114. constructor create;virtual;
  115. function pass_1 : tnode;override;
  116. function det_resulttype:tnode;override;
  117. end;
  118. tnilnodeclass = class of tnilnode;
  119. tguidconstnode = class(tnode)
  120. value : tguid;
  121. constructor create(const g:tguid);virtual;
  122. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  123. procedure ppuwrite(ppufile:tcompilerppufile);override;
  124. function getcopy : tnode;override;
  125. function pass_1 : tnode;override;
  126. function det_resulttype:tnode;override;
  127. function docompare(p: tnode) : boolean; override;
  128. end;
  129. tguidconstnodeclass = class of tguidconstnode;
  130. var
  131. crealconstnode : trealconstnodeclass;
  132. cordconstnode : tordconstnodeclass;
  133. cpointerconstnode : tpointerconstnodeclass;
  134. cstringconstnode : tstringconstnodeclass;
  135. csetconstnode : tsetconstnodeclass;
  136. cguidconstnode : tguidconstnodeclass;
  137. cnilnode : tnilnodeclass;
  138. function genintconstnode(v : TConstExprInt) : tordconstnode;
  139. function genenumnode(v : tenumsym) : tordconstnode;
  140. { some helper routines }
  141. {$ifdef INT64FUNCRESOK}
  142. function get_ordinal_value(p : tnode) : TConstExprInt;
  143. {$else INT64FUNCRESOK}
  144. function get_ordinal_value(p : tnode) : longint;
  145. {$endif INT64FUNCRESOK}
  146. function is_constresourcestringnode(p : tnode) : boolean;
  147. function str_length(p : tnode) : longint;
  148. function is_emptyset(p : tnode):boolean;
  149. function genconstsymtree(p : tconstsym) : tnode;
  150. implementation
  151. uses
  152. cutils,
  153. verbose,systems,
  154. defutil,
  155. cpubase,cgbase,
  156. nld;
  157. function genintconstnode(v : TConstExprInt) : tordconstnode;
  158. var
  159. htype : ttype;
  160. begin
  161. int_to_type(v,htype);
  162. genintconstnode:=cordconstnode.create(v,htype,true);
  163. end;
  164. function genenumnode(v : tenumsym) : tordconstnode;
  165. var
  166. htype : ttype;
  167. begin
  168. htype.setdef(v.definition);
  169. genenumnode:=cordconstnode.create(v.value,htype,true);
  170. end;
  171. {$ifdef INT64FUNCRESOK}
  172. function get_ordinal_value(p : tnode) : TConstExprInt;
  173. {$else INT64FUNCRESOK}
  174. function get_ordinal_value(p : tnode) : longint;
  175. {$endif INT64FUNCRESOK}
  176. begin
  177. get_ordinal_value:=0;
  178. if is_constnode(p) then
  179. begin
  180. if p.nodetype=ordconstn then
  181. get_ordinal_value:=tordconstnode(p).value
  182. else
  183. Message(type_e_ordinal_expr_expected);
  184. end
  185. else
  186. Message(type_e_constant_expr_expected);
  187. end;
  188. function is_constresourcestringnode(p : tnode) : boolean;
  189. begin
  190. is_constresourcestringnode:=(p.nodetype=loadn) and
  191. (tloadnode(p).symtableentry.typ=constsym) and
  192. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  193. end;
  194. function str_length(p : tnode) : longint;
  195. begin
  196. str_length:=tstringconstnode(p).len;
  197. end;
  198. function is_emptyset(p : tnode):boolean;
  199. begin
  200. is_emptyset:=(p.nodetype=setconstn) and
  201. (Tsetconstnode(p).value_set^=[]);
  202. end;
  203. function genconstsymtree(p : tconstsym) : tnode;
  204. var
  205. p1 : tnode;
  206. len : longint;
  207. pc : pchar;
  208. begin
  209. p1:=nil;
  210. case p.consttyp of
  211. constord :
  212. p1:=cordconstnode.create(p.value.valueord,p.consttype,true);
  213. conststring :
  214. begin
  215. len:=p.value.len;
  216. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  217. len:=255;
  218. getmem(pc,len+1);
  219. move(pchar(p.value.valueptr)^,pc^,len);
  220. pc[len]:=#0;
  221. p1:=cstringconstnode.createpchar(pc,len);
  222. end;
  223. constreal :
  224. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
  225. constset :
  226. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.consttype);
  227. constpointer :
  228. p1:=cpointerconstnode.create(p.value.valueordptr,p.consttype);
  229. constnil :
  230. p1:=cnilnode.create;
  231. else
  232. internalerror(200205103);
  233. end;
  234. genconstsymtree:=p1;
  235. end;
  236. {*****************************************************************************
  237. TREALCONSTNODE
  238. *****************************************************************************}
  239. { generic code }
  240. { overridden by: }
  241. { i386 }
  242. constructor trealconstnode.create(v : bestreal;const t:ttype);
  243. begin
  244. inherited create(realconstn);
  245. restype:=t;
  246. value_real:=v;
  247. lab_real:=nil;
  248. end;
  249. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  250. begin
  251. inherited ppuload(t,ppufile);
  252. ppufile.gettype(restype);
  253. value_real:=ppufile.getreal;
  254. lab_real:=tasmlabel(ppufile.getasmsymbol);
  255. end;
  256. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  257. begin
  258. inherited ppuwrite(ppufile);
  259. ppufile.puttype(restype);
  260. ppufile.putreal(value_real);
  261. ppufile.putasmsymbol(lab_real);
  262. end;
  263. procedure trealconstnode.buildderefimpl;
  264. begin
  265. inherited buildderefimpl;
  266. restype.buildderef;
  267. end;
  268. procedure trealconstnode.derefimpl;
  269. begin
  270. inherited derefimpl;
  271. restype.resolve;
  272. objectlibrary.derefasmsymbol(tasmsymbol(lab_real));
  273. end;
  274. function trealconstnode.getcopy : tnode;
  275. var
  276. n : trealconstnode;
  277. begin
  278. n:=trealconstnode(inherited getcopy);
  279. n.value_real:=value_real;
  280. n.lab_real:=lab_real;
  281. getcopy:=n;
  282. end;
  283. function trealconstnode.det_resulttype:tnode;
  284. begin
  285. result:=nil;
  286. resulttype:=restype;
  287. end;
  288. function trealconstnode.pass_1 : tnode;
  289. begin
  290. result:=nil;
  291. expectloc:=LOC_CREFERENCE;
  292. { needs to be loaded into an FPU register }
  293. registersfpu:=1;
  294. end;
  295. function trealconstnode.docompare(p: tnode): boolean;
  296. begin
  297. docompare :=
  298. inherited docompare(p) and
  299. (value_real = trealconstnode(p).value_real) and
  300. { floating point compares for non-numbers give strange results usually }
  301. is_number_float(value_real) and
  302. is_number_float(trealconstnode(p).value_real);
  303. end;
  304. procedure Trealconstnode.printnodedata(var t:text);
  305. begin
  306. inherited printnodedata(t);
  307. writeln(t,printnodeindention,'value = ',value_real);
  308. end;
  309. {*****************************************************************************
  310. TORDCONSTNODE
  311. *****************************************************************************}
  312. constructor tordconstnode.create(v : tconstexprint;const t:ttype;_rangecheck : boolean);
  313. begin
  314. inherited create(ordconstn);
  315. value:=v;
  316. restype:=t;
  317. rangecheck := _rangecheck;
  318. end;
  319. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  320. begin
  321. inherited ppuload(t,ppufile);
  322. ppufile.gettype(restype);
  323. value:=ppufile.getexprint;
  324. { normally, the value is already compiled, so we don't need
  325. to do once again a range check
  326. }
  327. rangecheck := false;
  328. end;
  329. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  330. begin
  331. inherited ppuwrite(ppufile);
  332. ppufile.puttype(restype);
  333. ppufile.putexprint(value);
  334. end;
  335. procedure tordconstnode.buildderefimpl;
  336. begin
  337. inherited buildderefimpl;
  338. restype.buildderef;
  339. end;
  340. procedure tordconstnode.derefimpl;
  341. begin
  342. inherited derefimpl;
  343. restype.resolve;
  344. end;
  345. function tordconstnode.getcopy : tnode;
  346. var
  347. n : tordconstnode;
  348. begin
  349. n:=tordconstnode(inherited getcopy);
  350. n.value:=value;
  351. n.restype := restype;
  352. getcopy:=n;
  353. end;
  354. function tordconstnode.det_resulttype:tnode;
  355. begin
  356. result:=nil;
  357. resulttype:=restype;
  358. { only do range checking when explicitly asked for it }
  359. if rangecheck then
  360. testrange(resulttype.def,value,false);
  361. end;
  362. function tordconstnode.pass_1 : tnode;
  363. begin
  364. result:=nil;
  365. expectloc:=LOC_CONSTANT;
  366. end;
  367. function tordconstnode.docompare(p: tnode): boolean;
  368. begin
  369. docompare :=
  370. inherited docompare(p) and
  371. (value = tordconstnode(p).value);
  372. end;
  373. procedure Tordconstnode.printnodedata(var t:text);
  374. begin
  375. inherited printnodedata(t);
  376. writeln(t,printnodeindention,'value = ',value);
  377. end;
  378. {*****************************************************************************
  379. TPOINTERCONSTNODE
  380. *****************************************************************************}
  381. constructor tpointerconstnode.create(v : TConstPtrUInt;const t:ttype);
  382. begin
  383. inherited create(pointerconstn);
  384. value:=v;
  385. restype:=t;
  386. end;
  387. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  388. begin
  389. inherited ppuload(t,ppufile);
  390. ppufile.gettype(restype);
  391. value:=ppufile.getptruint;
  392. end;
  393. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  394. begin
  395. inherited ppuwrite(ppufile);
  396. ppufile.puttype(restype);
  397. ppufile.putptruint(value);
  398. end;
  399. procedure tpointerconstnode.buildderefimpl;
  400. begin
  401. inherited buildderefimpl;
  402. restype.buildderef;
  403. end;
  404. procedure tpointerconstnode.derefimpl;
  405. begin
  406. inherited derefimpl;
  407. restype.resolve;
  408. end;
  409. function tpointerconstnode.getcopy : tnode;
  410. var
  411. n : tpointerconstnode;
  412. begin
  413. n:=tpointerconstnode(inherited getcopy);
  414. n.value:=value;
  415. n.restype := restype;
  416. getcopy:=n;
  417. end;
  418. function tpointerconstnode.det_resulttype:tnode;
  419. begin
  420. result:=nil;
  421. resulttype:=restype;
  422. end;
  423. function tpointerconstnode.pass_1 : tnode;
  424. begin
  425. result:=nil;
  426. expectloc:=LOC_CONSTANT;
  427. end;
  428. function tpointerconstnode.docompare(p: tnode): boolean;
  429. begin
  430. docompare :=
  431. inherited docompare(p) and
  432. (value = tpointerconstnode(p).value);
  433. end;
  434. {*****************************************************************************
  435. TSTRINGCONSTNODE
  436. *****************************************************************************}
  437. constructor tstringconstnode.createstr(const s : string;st:tstringtype);
  438. var
  439. l : longint;
  440. begin
  441. inherited create(stringconstn);
  442. l:=length(s);
  443. len:=l;
  444. { stringdup write even past a #0 }
  445. getmem(value_str,l+1);
  446. move(s[1],value_str^,l);
  447. value_str[l]:=#0;
  448. lab_str:=nil;
  449. if st=st_default then
  450. begin
  451. if cs_ansistrings in aktlocalswitches then
  452. {$ifdef ansistring_bits}
  453. case aktansistring_bits of
  454. sb_16:
  455. st_type:=st_ansistring16;
  456. sb_32:
  457. st_type:=st_ansistring32;
  458. sb_64:
  459. st_type:=st_ansistring64;
  460. end
  461. {$else}
  462. st_type:=st_ansistring
  463. {$endif}
  464. else
  465. st_type:=st_shortstring;
  466. end
  467. else
  468. st_type:=st;
  469. end;
  470. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  471. begin
  472. inherited create(stringconstn);
  473. len:=getlengthwidestring(w);
  474. initwidestring(pcompilerwidestring(value_str));
  475. copywidestring(w,pcompilerwidestring(value_str));
  476. lab_str:=nil;
  477. st_type:=st_widestring;
  478. end;
  479. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  480. begin
  481. inherited create(stringconstn);
  482. len:=l;
  483. value_str:=s;
  484. if (cs_ansistrings in aktlocalswitches) or
  485. (len>255) then
  486. {$ifdef ansistring_bits}
  487. case aktansistring_bits of
  488. sb_16:
  489. st_type:=st_ansistring16;
  490. sb_32:
  491. st_type:=st_ansistring32;
  492. sb_64:
  493. st_type:=st_ansistring64;
  494. end
  495. {$else}
  496. st_type:=st_ansistring
  497. {$endif}
  498. else
  499. st_type:=st_shortstring;
  500. lab_str:=nil;
  501. end;
  502. destructor tstringconstnode.destroy;
  503. begin
  504. if st_type=st_widestring then
  505. donewidestring(pcompilerwidestring(value_str))
  506. else
  507. ansistringdispose(value_str,len);
  508. inherited destroy;
  509. end;
  510. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  511. begin
  512. inherited ppuload(t,ppufile);
  513. st_type:=tstringtype(ppufile.getbyte);
  514. len:=ppufile.getlongint;
  515. getmem(value_str,len+1);
  516. ppufile.getdata(value_str^,len);
  517. value_str[len]:=#0;
  518. lab_str:=tasmlabel(ppufile.getasmsymbol);
  519. end;
  520. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  521. begin
  522. inherited ppuwrite(ppufile);
  523. ppufile.putbyte(byte(st_type));
  524. ppufile.putlongint(len);
  525. ppufile.putdata(value_str^,len);
  526. ppufile.putasmsymbol(lab_str);
  527. end;
  528. procedure tstringconstnode.buildderefimpl;
  529. begin
  530. inherited buildderefimpl;
  531. end;
  532. procedure tstringconstnode.derefimpl;
  533. begin
  534. inherited derefimpl;
  535. objectlibrary.derefasmsymbol(tasmsymbol(lab_str));
  536. end;
  537. function tstringconstnode.getcopy : tnode;
  538. var
  539. n : tstringconstnode;
  540. begin
  541. n:=tstringconstnode(inherited getcopy);
  542. n.st_type:=st_type;
  543. n.len:=len;
  544. n.lab_str:=lab_str;
  545. if st_type=st_widestring then
  546. begin
  547. initwidestring(pcompilerwidestring(n.value_str));
  548. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  549. end
  550. else
  551. n.value_str:=getpcharcopy;
  552. getcopy:=n;
  553. end;
  554. function tstringconstnode.det_resulttype:tnode;
  555. begin
  556. result:=nil;
  557. case st_type of
  558. st_shortstring :
  559. resulttype:=cshortstringtype;
  560. {$ifdef ansistring_bits}
  561. st_ansistring16:
  562. resulttype:=cansistringtype16;
  563. st_ansistring32:
  564. resulttype:=cansistringtype32;
  565. st_ansistring64:
  566. resulttype:=cansistringtype64;
  567. {$else}
  568. st_ansistring :
  569. resulttype:=cansistringtype;
  570. {$endif}
  571. st_widestring :
  572. resulttype:=cwidestringtype;
  573. st_longstring :
  574. resulttype:=clongstringtype;
  575. end;
  576. end;
  577. function tstringconstnode.pass_1 : tnode;
  578. begin
  579. result:=nil;
  580. {$ifdef ansistring_bits}
  581. if (st_type in [st_ansistring16,st_ansistring32,st_ansistring64,st_widestring]) and
  582. {$else}
  583. if (st_type in [st_ansistring,st_widestring]) and
  584. {$endif}
  585. (len=0) then
  586. expectloc:=LOC_CONSTANT
  587. else
  588. expectloc:=LOC_CREFERENCE;
  589. end;
  590. function tstringconstnode.getpcharcopy : pchar;
  591. var
  592. pc : pchar;
  593. begin
  594. pc:=nil;
  595. getmem(pc,len+1);
  596. if pc=nil then
  597. Message(general_f_no_memory_left);
  598. move(value_str^,pc^,len+1);
  599. getpcharcopy:=pc;
  600. end;
  601. function tstringconstnode.docompare(p: tnode): boolean;
  602. begin
  603. docompare :=
  604. inherited docompare(p) and
  605. (len = tstringconstnode(p).len) and
  606. { Don't compare the pchars, since they may contain null chars }
  607. { Since all equal constant strings are replaced by the same }
  608. { label, the following compare should be enough (JM) }
  609. (lab_str = tstringconstnode(p).lab_str);
  610. end;
  611. {*****************************************************************************
  612. TSETCONSTNODE
  613. *****************************************************************************}
  614. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  615. begin
  616. inherited create(setconstn,nil);
  617. restype:=t;
  618. if assigned(s) then
  619. begin
  620. new(value_set);
  621. value_set^:=s^;
  622. end
  623. else
  624. value_set:=nil;
  625. end;
  626. destructor tsetconstnode.destroy;
  627. begin
  628. if assigned(value_set) then
  629. dispose(value_set);
  630. inherited destroy;
  631. end;
  632. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  633. begin
  634. inherited ppuload(t,ppufile);
  635. ppufile.gettype(restype);
  636. new(value_set);
  637. ppufile.getdata(value_set^,sizeof(tconstset));
  638. end;
  639. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  640. begin
  641. inherited ppuwrite(ppufile);
  642. ppufile.puttype(restype);
  643. ppufile.putdata(value_set^,sizeof(tconstset));
  644. end;
  645. procedure tsetconstnode.buildderefimpl;
  646. begin
  647. inherited buildderefimpl;
  648. restype.buildderef;
  649. end;
  650. procedure tsetconstnode.derefimpl;
  651. begin
  652. inherited derefimpl;
  653. restype.resolve;
  654. end;
  655. function tsetconstnode.getcopy : tnode;
  656. var
  657. n : tsetconstnode;
  658. begin
  659. n:=tsetconstnode(inherited getcopy);
  660. if assigned(value_set) then
  661. begin
  662. new(n.value_set);
  663. n.value_set^:=value_set^
  664. end
  665. else
  666. n.value_set:=nil;
  667. n.restype := restype;
  668. n.lab_set:=lab_set;
  669. getcopy:=n;
  670. end;
  671. function tsetconstnode.det_resulttype:tnode;
  672. begin
  673. result:=nil;
  674. resulttype:=restype;
  675. end;
  676. function tsetconstnode.pass_1 : tnode;
  677. begin
  678. result:=nil;
  679. if tsetdef(resulttype.def).settype=smallset then
  680. expectloc:=LOC_CONSTANT
  681. else
  682. expectloc:=LOC_CREFERENCE;
  683. end;
  684. function tsetconstnode.docompare(p: tnode): boolean;
  685. begin
  686. docompare:=(inherited docompare(p)) and
  687. (value_set^=Tsetconstnode(p).value_set^);
  688. end;
  689. {*****************************************************************************
  690. TNILNODE
  691. *****************************************************************************}
  692. constructor tnilnode.create;
  693. begin
  694. inherited create(niln);
  695. end;
  696. function tnilnode.det_resulttype:tnode;
  697. begin
  698. result:=nil;
  699. resulttype:=voidpointertype;
  700. end;
  701. function tnilnode.pass_1 : tnode;
  702. begin
  703. result:=nil;
  704. expectloc:=LOC_CONSTANT;
  705. end;
  706. {*****************************************************************************
  707. TGUIDCONSTNODE
  708. *****************************************************************************}
  709. constructor tguidconstnode.create(const g:tguid);
  710. begin
  711. inherited create(guidconstn);
  712. value:=g;
  713. end;
  714. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  715. begin
  716. inherited ppuload(t,ppufile);
  717. ppufile.getguid(value);
  718. end;
  719. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  720. begin
  721. inherited ppuwrite(ppufile);
  722. ppufile.putguid(value);
  723. end;
  724. function tguidconstnode.getcopy : tnode;
  725. var
  726. n : tguidconstnode;
  727. begin
  728. n:=tguidconstnode(inherited getcopy);
  729. n.value:=value;
  730. getcopy:=n;
  731. end;
  732. function tguidconstnode.det_resulttype:tnode;
  733. begin
  734. result:=nil;
  735. resulttype.setdef(rec_tguid);
  736. end;
  737. function tguidconstnode.pass_1 : tnode;
  738. begin
  739. result:=nil;
  740. expectloc:=LOC_CREFERENCE;
  741. end;
  742. function tguidconstnode.docompare(p: tnode): boolean;
  743. begin
  744. docompare :=
  745. inherited docompare(p) and
  746. (guid2string(value) = guid2string(tguidconstnode(p).value));
  747. end;
  748. begin
  749. crealconstnode:=trealconstnode;
  750. cordconstnode:=tordconstnode;
  751. cpointerconstnode:=tpointerconstnode;
  752. cstringconstnode:=tstringconstnode;
  753. csetconstnode:=tsetconstnode;
  754. cnilnode:=tnilnode;
  755. cguidconstnode:=tguidconstnode;
  756. end.
  757. {
  758. $Log$
  759. Revision 1.62 2004-05-24 20:39:41 florian
  760. * stricter handling of formal const parameters and IE fixed
  761. Revision 1.61 2004/04/29 19:56:37 daniel
  762. * Prepare compiler infrastructure for multiple ansistring types
  763. Revision 1.60 2004/03/23 22:34:49 peter
  764. * constants ordinals now always have a type assigned
  765. * integer constants have the smallest type, unsigned prefered over
  766. signed
  767. Revision 1.59 2004/02/03 22:32:54 peter
  768. * renamed xNNbittype to xNNinttype
  769. * renamed registers32 to registersint
  770. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  771. Revision 1.58 2004/01/26 16:12:27 daniel
  772. * reginfo now also only allocated during register allocation
  773. * third round of gdb cleanups: kick out most of concatstabto
  774. Revision 1.57 2004/01/12 16:35:40 peter
  775. * range check error
  776. Revision 1.56 2003/10/23 14:44:07 peter
  777. * splitted buildderef and buildderefimpl to fix interface crc
  778. calculation
  779. Revision 1.55 2003/10/22 20:40:00 peter
  780. * write derefdata in a separate ppu entry
  781. Revision 1.54 2003/10/07 18:17:44 peter
  782. * Give message that constant expr is expected when a none constant
  783. is passed to get_ordinal_value
  784. Revision 1.53 2003/10/02 21:18:44 peter
  785. * niln is also a constnode
  786. Revision 1.52 2003/10/01 20:34:48 peter
  787. * procinfo unit contains tprocinfo
  788. * cginfo renamed to cgbase
  789. * moved cgmessage to verbose
  790. * fixed ppc and sparc compiles
  791. Revision 1.51 2003/09/06 16:47:24 florian
  792. + support of NaN and Inf in the compiler as values of real constants
  793. Revision 1.50 2003/09/03 15:55:01 peter
  794. * NEWRA branch merged
  795. Revision 1.49 2003/04/25 20:59:33 peter
  796. * removed funcretn,funcretsym, function result is now in varsym
  797. and aliases for result and function name are added using absolutesym
  798. * vs_hidden parameter for funcret passed in parameter
  799. * vs_hidden fixes
  800. * writenode changed to printnode and released from extdebug
  801. * -vp option added to generate a tree.log with the nodetree
  802. * nicer printnode for statements, callnode
  803. Revision 1.48 2003/04/24 22:29:57 florian
  804. * fixed a lot of PowerPC related stuff
  805. Revision 1.47 2003/04/23 20:16:04 peter
  806. + added currency support based on int64
  807. + is_64bit for use in cg units instead of is_64bitint
  808. * removed cgmessage from n386add, replace with internalerrors
  809. Revision 1.46 2003/04/22 23:50:23 peter
  810. * firstpass uses expectloc
  811. * checks if there are differences between the expectloc and
  812. location.loc from secondpass in EXTDEBUG
  813. Revision 1.45 2002/11/25 17:43:18 peter
  814. * splitted defbase in defutil,symutil,defcmp
  815. * merged isconvertable and is_equal into compare_defs(_ext)
  816. * made operator search faster by walking the list only once
  817. Revision 1.44 2002/11/22 22:48:10 carl
  818. * memory optimization with tconstsym (1.5%)
  819. Revision 1.43 2002/10/05 12:43:25 carl
  820. * fixes for Delphi 6 compilation
  821. (warning : Some features do not work under Delphi)
  822. Revision 1.42 2002/09/07 15:25:03 peter
  823. * old logs removed and tabs fixed
  824. Revision 1.41 2002/09/07 12:16:04 carl
  825. * second part bug report 1996 fix, testrange in cordconstnode
  826. only called if option is set (also make parsing a tiny faster)
  827. Revision 1.40 2002/08/22 11:21:44 florian
  828. + register32 is now written by tnode.dowrite
  829. * fixed write of value of tconstnode
  830. Revision 1.39 2002/08/18 20:06:23 peter
  831. * inlining is now also allowed in interface
  832. * renamed write/load to ppuwrite/ppuload
  833. * tnode storing in ppu
  834. * nld,ncon,nbas are already updated for storing in ppu
  835. Revision 1.38 2002/08/17 22:09:45 florian
  836. * result type handling in tcgcal.pass_2 overhauled
  837. * better tnode.dowrite
  838. * some ppc stuff fixed
  839. Revision 1.37 2002/07/23 12:34:30 daniel
  840. * Readded old set code. To use it define 'oldset'. Activated by default
  841. for ppc.
  842. Revision 1.36 2002/07/22 11:48:04 daniel
  843. * Sets are now internally sets.
  844. Revision 1.35 2002/07/20 11:57:54 florian
  845. * types.pas renamed to defbase.pas because D6 contains a types
  846. unit so this would conflicts if D6 programms are compiled
  847. + Willamette/SSE2 instructions to assembler added
  848. Revision 1.34 2002/07/14 18:00:43 daniel
  849. + Added the beginning of a state tracker. This will track the values of
  850. variables through procedures and optimize things away.
  851. Revision 1.33 2002/07/01 18:46:23 peter
  852. * internal linker
  853. * reorganized aasm layer
  854. Revision 1.32 2002/05/18 13:34:09 peter
  855. * readded missing revisions
  856. Revision 1.31 2002/05/16 19:46:37 carl
  857. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  858. + try to fix temp allocation (still in ifdef)
  859. + generic constructor calls
  860. + start of tassembler / tmodulebase class cleanup
  861. Revision 1.29 2002/05/12 16:53:07 peter
  862. * moved entry and exitcode to ncgutil and cgobj
  863. * foreach gets extra argument for passing local data to the
  864. iterator function
  865. * -CR checks also class typecasts at runtime by changing them
  866. into as
  867. * fixed compiler to cycle with the -CR option
  868. * fixed stabs with elf writer, finally the global variables can
  869. be watched
  870. * removed a lot of routines from cga unit and replaced them by
  871. calls to cgobj
  872. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  873. u32bit then the other is typecasted also to u32bit without giving
  874. a rangecheck warning/error.
  875. * fixed pascal calling method with reversing also the high tree in
  876. the parast, detected by tcalcst3 test
  877. Revision 1.28 2002/04/07 13:25:20 carl
  878. + change unit use
  879. Revision 1.27 2002/04/04 19:05:58 peter
  880. * removed unused units
  881. * use tlocation.size in cg.a_*loc*() routines
  882. Revision 1.26 2002/04/02 17:11:29 peter
  883. * tlocation,treference update
  884. * LOC_CONSTANT added for better constant handling
  885. * secondadd splitted in multiple routines
  886. * location_force_reg added for loading a location to a register
  887. of a specified size
  888. * secondassignment parses now first the right and then the left node
  889. (this is compatible with Kylix). This saves a lot of push/pop especially
  890. with string operations
  891. * adapted some routines to use the new cg methods
  892. Revision 1.25 2002/03/04 19:10:11 peter
  893. * removed compiler warnings
  894. }