ncon.pas 30 KB

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