ncon.pas 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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) and
  333. { floating point compares for non-numbers give strange results usually }
  334. is_number_float(value_real) and
  335. is_number_float(trealconstnode(p).value_real);
  336. end;
  337. procedure Trealconstnode.printnodedata(var t:text);
  338. begin
  339. inherited printnodedata(t);
  340. writeln(t,printnodeindention,'value = ',value_real);
  341. end;
  342. {*****************************************************************************
  343. TORDCONSTNODE
  344. *****************************************************************************}
  345. constructor tordconstnode.create(v : tconstexprint;const t:ttype;_rangecheck : boolean);
  346. begin
  347. inherited create(ordconstn);
  348. value:=v;
  349. restype:=t;
  350. rangecheck := _rangecheck;
  351. end;
  352. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  353. begin
  354. inherited ppuload(t,ppufile);
  355. ppufile.gettype(restype);
  356. value:=ppufile.getexprint;
  357. { normally, the value is already compiled, so we don't need
  358. to do once again a range check
  359. }
  360. rangecheck := false;
  361. end;
  362. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  363. begin
  364. inherited ppuwrite(ppufile);
  365. ppufile.puttype(restype);
  366. ppufile.putexprint(value);
  367. end;
  368. procedure tordconstnode.derefimpl;
  369. begin
  370. inherited derefimpl;
  371. restype.resolve;
  372. end;
  373. function tordconstnode.getcopy : tnode;
  374. var
  375. n : tordconstnode;
  376. begin
  377. n:=tordconstnode(inherited getcopy);
  378. n.value:=value;
  379. n.restype := restype;
  380. getcopy:=n;
  381. end;
  382. function tordconstnode.det_resulttype:tnode;
  383. begin
  384. result:=nil;
  385. resulttype:=restype;
  386. { only do range checking when explicitly asked for it }
  387. if rangecheck then
  388. testrange(resulttype.def,value,false);
  389. end;
  390. function tordconstnode.pass_1 : tnode;
  391. begin
  392. result:=nil;
  393. expectloc:=LOC_CONSTANT;
  394. end;
  395. function tordconstnode.docompare(p: tnode): boolean;
  396. begin
  397. docompare :=
  398. inherited docompare(p) and
  399. (value = tordconstnode(p).value);
  400. end;
  401. procedure Tordconstnode.printnodedata(var t:text);
  402. begin
  403. inherited printnodedata(t);
  404. writeln(t,printnodeindention,'value = ',value);
  405. end;
  406. {*****************************************************************************
  407. TPOINTERCONSTNODE
  408. *****************************************************************************}
  409. constructor tpointerconstnode.create(v : TConstPtrUInt;const t:ttype);
  410. begin
  411. inherited create(pointerconstn);
  412. value:=v;
  413. restype:=t;
  414. end;
  415. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  416. begin
  417. inherited ppuload(t,ppufile);
  418. ppufile.gettype(restype);
  419. value:=ppufile.getptruint;
  420. end;
  421. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  422. begin
  423. inherited ppuwrite(ppufile);
  424. ppufile.puttype(restype);
  425. ppufile.putptruint(value);
  426. end;
  427. procedure tpointerconstnode.derefimpl;
  428. begin
  429. inherited derefimpl;
  430. restype.resolve;
  431. end;
  432. function tpointerconstnode.getcopy : tnode;
  433. var
  434. n : tpointerconstnode;
  435. begin
  436. n:=tpointerconstnode(inherited getcopy);
  437. n.value:=value;
  438. n.restype := restype;
  439. getcopy:=n;
  440. end;
  441. function tpointerconstnode.det_resulttype:tnode;
  442. begin
  443. result:=nil;
  444. resulttype:=restype;
  445. end;
  446. function tpointerconstnode.pass_1 : tnode;
  447. begin
  448. result:=nil;
  449. expectloc:=LOC_CONSTANT;
  450. end;
  451. function tpointerconstnode.docompare(p: tnode): boolean;
  452. begin
  453. docompare :=
  454. inherited docompare(p) and
  455. (value = tpointerconstnode(p).value);
  456. end;
  457. {*****************************************************************************
  458. TSTRINGCONSTNODE
  459. *****************************************************************************}
  460. constructor tstringconstnode.createstr(const s : string;st:tstringtype);
  461. var
  462. l : longint;
  463. begin
  464. inherited create(stringconstn);
  465. l:=length(s);
  466. len:=l;
  467. { stringdup write even past a #0 }
  468. getmem(value_str,l+1);
  469. move(s[1],value_str^,l);
  470. value_str[l]:=#0;
  471. lab_str:=nil;
  472. if st=st_default then
  473. begin
  474. if cs_ansistrings in aktlocalswitches then
  475. st_type:=st_ansistring
  476. else
  477. st_type:=st_shortstring;
  478. end
  479. else
  480. st_type:=st;
  481. end;
  482. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  483. begin
  484. inherited create(stringconstn);
  485. len:=getlengthwidestring(w);
  486. initwidestring(pcompilerwidestring(value_str));
  487. copywidestring(w,pcompilerwidestring(value_str));
  488. lab_str:=nil;
  489. st_type:=st_widestring;
  490. end;
  491. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  492. begin
  493. inherited create(stringconstn);
  494. len:=l;
  495. value_str:=s;
  496. if (cs_ansistrings in aktlocalswitches) or
  497. (len>255) then
  498. st_type:=st_ansistring
  499. else
  500. st_type:=st_shortstring;
  501. lab_str:=nil;
  502. end;
  503. destructor tstringconstnode.destroy;
  504. begin
  505. if st_type=st_widestring then
  506. donewidestring(pcompilerwidestring(value_str))
  507. else
  508. ansistringdispose(value_str,len);
  509. inherited destroy;
  510. end;
  511. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  512. begin
  513. inherited ppuload(t,ppufile);
  514. st_type:=tstringtype(ppufile.getbyte);
  515. len:=ppufile.getlongint;
  516. getmem(value_str,len+1);
  517. ppufile.getdata(value_str^,len);
  518. value_str[len]:=#0;
  519. lab_str:=tasmlabel(ppufile.getasmsymbol);
  520. end;
  521. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  522. begin
  523. inherited ppuwrite(ppufile);
  524. ppufile.putbyte(byte(st_type));
  525. ppufile.putlongint(len);
  526. ppufile.putdata(value_str^,len);
  527. ppufile.putasmsymbol(lab_str);
  528. end;
  529. procedure tstringconstnode.derefimpl;
  530. begin
  531. inherited derefimpl;
  532. objectlibrary.derefasmsymbol(tasmsymbol(lab_str));
  533. end;
  534. function tstringconstnode.getcopy : tnode;
  535. var
  536. n : tstringconstnode;
  537. begin
  538. n:=tstringconstnode(inherited getcopy);
  539. n.st_type:=st_type;
  540. n.len:=len;
  541. n.lab_str:=lab_str;
  542. if st_type=st_widestring then
  543. begin
  544. initwidestring(pcompilerwidestring(n.value_str));
  545. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  546. end
  547. else
  548. n.value_str:=getpcharcopy;
  549. getcopy:=n;
  550. end;
  551. function tstringconstnode.det_resulttype:tnode;
  552. begin
  553. result:=nil;
  554. case st_type of
  555. st_shortstring :
  556. resulttype:=cshortstringtype;
  557. st_ansistring :
  558. resulttype:=cansistringtype;
  559. st_widestring :
  560. resulttype:=cwidestringtype;
  561. st_longstring :
  562. resulttype:=clongstringtype;
  563. end;
  564. end;
  565. function tstringconstnode.pass_1 : tnode;
  566. begin
  567. result:=nil;
  568. if (st_type in [st_ansistring,st_widestring]) and
  569. (len=0) then
  570. expectloc:=LOC_CONSTANT
  571. else
  572. expectloc:=LOC_CREFERENCE;
  573. end;
  574. function tstringconstnode.getpcharcopy : pchar;
  575. var
  576. pc : pchar;
  577. begin
  578. pc:=nil;
  579. getmem(pc,len+1);
  580. if pc=nil then
  581. Message(general_f_no_memory_left);
  582. move(value_str^,pc^,len+1);
  583. getpcharcopy:=pc;
  584. end;
  585. function tstringconstnode.docompare(p: tnode): boolean;
  586. begin
  587. docompare :=
  588. inherited docompare(p) and
  589. (len = tstringconstnode(p).len) and
  590. { Don't compare the pchars, since they may contain null chars }
  591. { Since all equal constant strings are replaced by the same }
  592. { label, the following compare should be enough (JM) }
  593. (lab_str = tstringconstnode(p).lab_str);
  594. end;
  595. {*****************************************************************************
  596. TSETCONSTNODE
  597. *****************************************************************************}
  598. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  599. begin
  600. inherited create(setconstn,nil);
  601. restype:=t;
  602. if assigned(s) then
  603. begin
  604. new(value_set);
  605. value_set^:=s^;
  606. end
  607. else
  608. value_set:=nil;
  609. end;
  610. destructor tsetconstnode.destroy;
  611. begin
  612. if assigned(value_set) then
  613. dispose(value_set);
  614. inherited destroy;
  615. end;
  616. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  617. begin
  618. inherited ppuload(t,ppufile);
  619. ppufile.gettype(restype);
  620. new(value_set);
  621. ppufile.getdata(value_set^,sizeof(tconstset));
  622. end;
  623. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  624. begin
  625. inherited ppuwrite(ppufile);
  626. ppufile.puttype(restype);
  627. ppufile.putdata(value_set^,sizeof(tconstset));
  628. end;
  629. procedure tsetconstnode.derefimpl;
  630. begin
  631. inherited derefimpl;
  632. restype.resolve;
  633. end;
  634. function tsetconstnode.getcopy : tnode;
  635. var
  636. n : tsetconstnode;
  637. begin
  638. n:=tsetconstnode(inherited getcopy);
  639. if assigned(value_set) then
  640. begin
  641. new(n.value_set);
  642. n.value_set^:=value_set^
  643. end
  644. else
  645. n.value_set:=nil;
  646. n.restype := restype;
  647. n.lab_set:=lab_set;
  648. getcopy:=n;
  649. end;
  650. function tsetconstnode.det_resulttype:tnode;
  651. begin
  652. result:=nil;
  653. resulttype:=restype;
  654. end;
  655. function tsetconstnode.pass_1 : tnode;
  656. begin
  657. result:=nil;
  658. if tsetdef(resulttype.def).settype=smallset then
  659. expectloc:=LOC_CONSTANT
  660. else
  661. expectloc:=LOC_CREFERENCE;
  662. end;
  663. function tsetconstnode.docompare(p: tnode): boolean;
  664. begin
  665. docompare:=(inherited docompare(p)) and
  666. (value_set^=Tsetconstnode(p).value_set^);
  667. end;
  668. {*****************************************************************************
  669. TNILNODE
  670. *****************************************************************************}
  671. constructor tnilnode.create;
  672. begin
  673. inherited create(niln);
  674. end;
  675. function tnilnode.det_resulttype:tnode;
  676. begin
  677. result:=nil;
  678. resulttype:=voidpointertype;
  679. end;
  680. function tnilnode.pass_1 : tnode;
  681. begin
  682. result:=nil;
  683. expectloc:=LOC_CONSTANT;
  684. end;
  685. {*****************************************************************************
  686. TGUIDCONSTNODE
  687. *****************************************************************************}
  688. constructor tguidconstnode.create(const g:tguid);
  689. begin
  690. inherited create(guidconstn);
  691. value:=g;
  692. end;
  693. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  694. begin
  695. inherited ppuload(t,ppufile);
  696. ppufile.getguid(value);
  697. end;
  698. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  699. begin
  700. inherited ppuwrite(ppufile);
  701. ppufile.putguid(value);
  702. end;
  703. function tguidconstnode.getcopy : tnode;
  704. var
  705. n : tguidconstnode;
  706. begin
  707. n:=tguidconstnode(inherited getcopy);
  708. n.value:=value;
  709. getcopy:=n;
  710. end;
  711. function tguidconstnode.det_resulttype:tnode;
  712. begin
  713. result:=nil;
  714. resulttype.setdef(rec_tguid);
  715. end;
  716. function tguidconstnode.pass_1 : tnode;
  717. begin
  718. result:=nil;
  719. expectloc:=LOC_CREFERENCE;
  720. end;
  721. function tguidconstnode.docompare(p: tnode): boolean;
  722. begin
  723. docompare :=
  724. inherited docompare(p) and
  725. (guid2string(value) = guid2string(tguidconstnode(p).value));
  726. end;
  727. begin
  728. crealconstnode:=trealconstnode;
  729. cordconstnode:=tordconstnode;
  730. cpointerconstnode:=tpointerconstnode;
  731. cstringconstnode:=tstringconstnode;
  732. csetconstnode:=tsetconstnode;
  733. cnilnode:=tnilnode;
  734. cguidconstnode:=tguidconstnode;
  735. end.
  736. {
  737. $Log$
  738. Revision 1.51 2003-09-06 16:47:24 florian
  739. + support of NaN and Inf in the compiler as values of real constants
  740. Revision 1.50 2003/09/03 15:55:01 peter
  741. * NEWRA branch merged
  742. Revision 1.49 2003/04/25 20:59:33 peter
  743. * removed funcretn,funcretsym, function result is now in varsym
  744. and aliases for result and function name are added using absolutesym
  745. * vs_hidden parameter for funcret passed in parameter
  746. * vs_hidden fixes
  747. * writenode changed to printnode and released from extdebug
  748. * -vp option added to generate a tree.log with the nodetree
  749. * nicer printnode for statements, callnode
  750. Revision 1.48 2003/04/24 22:29:57 florian
  751. * fixed a lot of PowerPC related stuff
  752. Revision 1.47 2003/04/23 20:16:04 peter
  753. + added currency support based on int64
  754. + is_64bit for use in cg units instead of is_64bitint
  755. * removed cgmessage from n386add, replace with internalerrors
  756. Revision 1.46 2003/04/22 23:50:23 peter
  757. * firstpass uses expectloc
  758. * checks if there are differences between the expectloc and
  759. location.loc from secondpass in EXTDEBUG
  760. Revision 1.45 2002/11/25 17:43:18 peter
  761. * splitted defbase in defutil,symutil,defcmp
  762. * merged isconvertable and is_equal into compare_defs(_ext)
  763. * made operator search faster by walking the list only once
  764. Revision 1.44 2002/11/22 22:48:10 carl
  765. * memory optimization with tconstsym (1.5%)
  766. Revision 1.43 2002/10/05 12:43:25 carl
  767. * fixes for Delphi 6 compilation
  768. (warning : Some features do not work under Delphi)
  769. Revision 1.42 2002/09/07 15:25:03 peter
  770. * old logs removed and tabs fixed
  771. Revision 1.41 2002/09/07 12:16:04 carl
  772. * second part bug report 1996 fix, testrange in cordconstnode
  773. only called if option is set (also make parsing a tiny faster)
  774. Revision 1.40 2002/08/22 11:21:44 florian
  775. + register32 is now written by tnode.dowrite
  776. * fixed write of value of tconstnode
  777. Revision 1.39 2002/08/18 20:06:23 peter
  778. * inlining is now also allowed in interface
  779. * renamed write/load to ppuwrite/ppuload
  780. * tnode storing in ppu
  781. * nld,ncon,nbas are already updated for storing in ppu
  782. Revision 1.38 2002/08/17 22:09:45 florian
  783. * result type handling in tcgcal.pass_2 overhauled
  784. * better tnode.dowrite
  785. * some ppc stuff fixed
  786. Revision 1.37 2002/07/23 12:34:30 daniel
  787. * Readded old set code. To use it define 'oldset'. Activated by default
  788. for ppc.
  789. Revision 1.36 2002/07/22 11:48:04 daniel
  790. * Sets are now internally sets.
  791. Revision 1.35 2002/07/20 11:57:54 florian
  792. * types.pas renamed to defbase.pas because D6 contains a types
  793. unit so this would conflicts if D6 programms are compiled
  794. + Willamette/SSE2 instructions to assembler added
  795. Revision 1.34 2002/07/14 18:00:43 daniel
  796. + Added the beginning of a state tracker. This will track the values of
  797. variables through procedures and optimize things away.
  798. Revision 1.33 2002/07/01 18:46:23 peter
  799. * internal linker
  800. * reorganized aasm layer
  801. Revision 1.32 2002/05/18 13:34:09 peter
  802. * readded missing revisions
  803. Revision 1.31 2002/05/16 19:46:37 carl
  804. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  805. + try to fix temp allocation (still in ifdef)
  806. + generic constructor calls
  807. + start of tassembler / tmodulebase class cleanup
  808. Revision 1.29 2002/05/12 16:53:07 peter
  809. * moved entry and exitcode to ncgutil and cgobj
  810. * foreach gets extra argument for passing local data to the
  811. iterator function
  812. * -CR checks also class typecasts at runtime by changing them
  813. into as
  814. * fixed compiler to cycle with the -CR option
  815. * fixed stabs with elf writer, finally the global variables can
  816. be watched
  817. * removed a lot of routines from cga unit and replaced them by
  818. calls to cgobj
  819. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  820. u32bit then the other is typecasted also to u32bit without giving
  821. a rangecheck warning/error.
  822. * fixed pascal calling method with reversing also the high tree in
  823. the parast, detected by tcalcst3 test
  824. Revision 1.28 2002/04/07 13:25:20 carl
  825. + change unit use
  826. Revision 1.27 2002/04/04 19:05:58 peter
  827. * removed unused units
  828. * use tlocation.size in cg.a_*loc*() routines
  829. Revision 1.26 2002/04/02 17:11:29 peter
  830. * tlocation,treference update
  831. * LOC_CONSTANT added for better constant handling
  832. * secondadd splitted in multiple routines
  833. * location_force_reg added for loading a location to a register
  834. of a specified size
  835. * secondassignment parses now first the right and then the left node
  836. (this is compatible with Kylix). This saves a lot of push/pop especially
  837. with string operations
  838. * adapted some routines to use the new cg methods
  839. Revision 1.25 2002/03/04 19:10:11 peter
  840. * removed compiler warnings
  841. }