2
0

ncon.pas 31 KB

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