ncon.pas 30 KB

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