ncon.pas 31 KB

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