ncon.pas 32 KB

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