ncon.pas 31 KB

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