ncon.pas 33 KB

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