ncon.pas 32 KB

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