ncon.pas 32 KB

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