ncon.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. {
  2. $Id$
  3. Copyright (c) 2000 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 defines.inc}
  20. interface
  21. uses
  22. globtype,widestr,
  23. node,
  24. aasm,cpuinfo,
  25. symconst,symtype,symdef,symsym;
  26. type
  27. trealconstnode = class(tnode)
  28. restype : ttype;
  29. value_real : bestreal;
  30. lab_real : tasmlabel;
  31. constructor create(v : bestreal;const t:ttype);virtual;
  32. function getcopy : tnode;override;
  33. function pass_1 : tnode;override;
  34. function det_resulttype:tnode;override;
  35. function docompare(p: tnode) : boolean; override;
  36. end;
  37. tordconstnode = class(tnode)
  38. restype : ttype;
  39. value : TConstExprInt;
  40. constructor create(v : tconstexprint;const t:ttype);virtual;
  41. function getcopy : tnode;override;
  42. function pass_1 : tnode;override;
  43. function det_resulttype:tnode;override;
  44. function docompare(p: tnode) : boolean; override;
  45. end;
  46. tpointerconstnode = class(tnode)
  47. restype : ttype;
  48. value : TPointerOrd;
  49. constructor create(v : tpointerord;const t:ttype);virtual;
  50. function getcopy : tnode;override;
  51. function pass_1 : tnode;override;
  52. function det_resulttype:tnode;override;
  53. function docompare(p: tnode) : boolean; override;
  54. end;
  55. tstringconstnode = class(tnode)
  56. value_str : pchar;
  57. len : longint;
  58. lab_str : tasmlabel;
  59. st_type : tstringtype;
  60. constructor createstr(const s : string;st:tstringtype);virtual;
  61. constructor createpchar(s : pchar;l : longint);virtual;
  62. constructor createwstr(const w : tcompilerwidestring);virtual;
  63. destructor destroy;override;
  64. function getcopy : tnode;override;
  65. function pass_1 : tnode;override;
  66. function det_resulttype:tnode;override;
  67. function getpcharcopy : pchar;
  68. function docompare(p: tnode) : boolean; override;
  69. end;
  70. tsetconstnode = class(tunarynode)
  71. restype : ttype;
  72. value_set : pconstset;
  73. lab_set : tasmlabel;
  74. constructor create(s : pconstset;const t:ttype);virtual;
  75. destructor destroy;override;
  76. function getcopy : tnode;override;
  77. function pass_1 : tnode;override;
  78. function det_resulttype:tnode;override;
  79. function docompare(p: tnode) : boolean; override;
  80. end;
  81. tnilnode = class(tnode)
  82. constructor create;virtual;
  83. function pass_1 : tnode;override;
  84. function det_resulttype:tnode;override;
  85. end;
  86. var
  87. crealconstnode : class of trealconstnode;
  88. cordconstnode : class of tordconstnode;
  89. cpointerconstnode : class of tpointerconstnode;
  90. cstringconstnode : class of tstringconstnode;
  91. csetconstnode : class of tsetconstnode;
  92. cnilnode : class of tnilnode;
  93. function genintconstnode(v : TConstExprInt) : tordconstnode;
  94. function genenumnode(v : tenumsym) : tordconstnode;
  95. { some helper routines }
  96. {$ifdef INT64FUNCRESOK}
  97. function get_ordinal_value(p : tnode) : TConstExprInt;
  98. {$else INT64FUNCRESOK}
  99. function get_ordinal_value(p : tnode) : longint;
  100. {$endif INT64FUNCRESOK}
  101. function is_constnode(p : tnode) : boolean;
  102. function is_constintnode(p : tnode) : boolean;
  103. function is_constcharnode(p : tnode) : boolean;
  104. function is_constrealnode(p : tnode) : boolean;
  105. function is_constboolnode(p : tnode) : boolean;
  106. function is_constresourcestringnode(p : tnode) : boolean;
  107. function str_length(p : tnode) : longint;
  108. function is_emptyset(p : tnode):boolean;
  109. function genconstsymtree(p : tconstsym) : tnode;
  110. implementation
  111. uses
  112. cutils,verbose,globals,systems,
  113. types,cpubase,nld;
  114. function genintconstnode(v : TConstExprInt) : tordconstnode;
  115. var
  116. i,i2 : TConstExprInt;
  117. begin
  118. { we need to bootstrap this code, so it's a little bit messy }
  119. i:=2147483647;
  120. { maxcardinal }
  121. i2 := i+i+1;
  122. if (v<=i) and (v>=-i-1) then
  123. genintconstnode:=cordconstnode.create(v,s32bittype)
  124. else if (v > i) and (v <= i2) then
  125. genintconstnode:=cordconstnode.create(v,u32bittype)
  126. else
  127. genintconstnode:=cordconstnode.create(v,cs64bittype);
  128. end;
  129. function genenumnode(v : tenumsym) : tordconstnode;
  130. var
  131. htype : ttype;
  132. begin
  133. htype.setdef(v.definition);
  134. genenumnode:=cordconstnode.create(v.value,htype);
  135. end;
  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. begin
  142. if p.nodetype=ordconstn then
  143. get_ordinal_value:=tordconstnode(p).value
  144. else
  145. begin
  146. Message(type_e_ordinal_expr_expected);
  147. get_ordinal_value:=0;
  148. end;
  149. end;
  150. function is_constnode(p : tnode) : boolean;
  151. begin
  152. is_constnode:=(p.nodetype in [ordconstn,realconstn,stringconstn,setconstn]);
  153. end;
  154. function is_constintnode(p : tnode) : boolean;
  155. begin
  156. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  157. end;
  158. function is_constcharnode(p : tnode) : boolean;
  159. begin
  160. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  161. end;
  162. function is_constrealnode(p : tnode) : boolean;
  163. begin
  164. is_constrealnode:=(p.nodetype=realconstn);
  165. end;
  166. function is_constboolnode(p : tnode) : boolean;
  167. begin
  168. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  169. end;
  170. function is_constresourcestringnode(p : tnode) : boolean;
  171. begin
  172. is_constresourcestringnode:=(p.nodetype=loadn) and
  173. (tloadnode(p).symtableentry.typ=constsym) and
  174. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  175. end;
  176. function str_length(p : tnode) : longint;
  177. begin
  178. str_length:=tstringconstnode(p).len;
  179. end;
  180. function is_emptyset(p : tnode):boolean;
  181. var
  182. i : longint;
  183. begin
  184. i:=0;
  185. if p.nodetype=setconstn then
  186. begin
  187. while (i<32) and (tsetconstnode(p).value_set^[i]=0) do
  188. inc(i);
  189. end;
  190. is_emptyset:=(i=32);
  191. end;
  192. function genconstsymtree(p : tconstsym) : tnode;
  193. var
  194. p1 : tnode;
  195. len : longint;
  196. pc : pchar;
  197. begin
  198. p1:=nil;
  199. case p.consttyp of
  200. constint :
  201. p1:=genintconstnode(p.value);
  202. conststring :
  203. begin
  204. len:=p.len;
  205. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  206. len:=255;
  207. getmem(pc,len+1);
  208. move(pchar(tpointerord(p.value))^,pc^,len);
  209. pc[len]:=#0;
  210. p1:=cstringconstnode.createpchar(pc,len);
  211. end;
  212. constchar :
  213. p1:=cordconstnode.create(p.value,cchartype);
  214. constreal :
  215. p1:=crealconstnode.create(pbestreal(tpointerord(p.value))^,pbestrealtype^);
  216. constbool :
  217. p1:=cordconstnode.create(p.value,booltype);
  218. constset :
  219. p1:=csetconstnode.create(pconstset(tpointerord(p.value)),p.consttype);
  220. constord :
  221. p1:=cordconstnode.create(p.value,p.consttype);
  222. constpointer :
  223. p1:=cpointerconstnode.create(p.value,p.consttype);
  224. constnil :
  225. p1:=cnilnode.create;
  226. constresourcestring:
  227. begin
  228. p1:=cloadnode.create(tvarsym(p),tvarsym(p).owner);
  229. p1.resulttype:=cansistringtype;
  230. end;
  231. end;
  232. genconstsymtree:=p1;
  233. end;
  234. {*****************************************************************************
  235. TREALCONSTNODE
  236. *****************************************************************************}
  237. constructor trealconstnode.create(v : bestreal;const t:ttype);
  238. begin
  239. inherited create(realconstn);
  240. restype:=t;
  241. value_real:=v;
  242. lab_real:=nil;
  243. end;
  244. function trealconstnode.getcopy : tnode;
  245. var
  246. n : trealconstnode;
  247. begin
  248. n:=trealconstnode(inherited getcopy);
  249. n.value_real:=value_real;
  250. n.lab_real:=lab_real;
  251. getcopy:=n;
  252. end;
  253. function trealconstnode.det_resulttype:tnode;
  254. begin
  255. result:=nil;
  256. resulttype:=restype;
  257. end;
  258. function trealconstnode.pass_1 : tnode;
  259. begin
  260. result:=nil;
  261. if (value_real=1.0) or (value_real=0.0) then
  262. begin
  263. location.loc:=LOC_FPU;
  264. registersfpu:=1;
  265. end
  266. else
  267. location.loc:=LOC_MEM;
  268. end;
  269. function trealconstnode.docompare(p: tnode): boolean;
  270. begin
  271. docompare :=
  272. inherited docompare(p) and
  273. (value_real = trealconstnode(p).value_real);
  274. end;
  275. {*****************************************************************************
  276. TORDCONSTNODE
  277. *****************************************************************************}
  278. constructor tordconstnode.create(v : tconstexprint;const t:ttype);
  279. begin
  280. inherited create(ordconstn);
  281. value:=v;
  282. restype:=t;
  283. end;
  284. function tordconstnode.getcopy : tnode;
  285. var
  286. n : tordconstnode;
  287. begin
  288. n:=tordconstnode(inherited getcopy);
  289. n.value:=value;
  290. getcopy:=n;
  291. end;
  292. function tordconstnode.det_resulttype:tnode;
  293. begin
  294. result:=nil;
  295. resulttype:=restype;
  296. testrange(resulttype.def,value,false);
  297. end;
  298. function tordconstnode.pass_1 : tnode;
  299. begin
  300. result:=nil;
  301. location.loc:=LOC_MEM;
  302. end;
  303. function tordconstnode.docompare(p: tnode): boolean;
  304. begin
  305. docompare :=
  306. inherited docompare(p) and
  307. (value = tordconstnode(p).value);
  308. end;
  309. {*****************************************************************************
  310. TPOINTERCONSTNODE
  311. *****************************************************************************}
  312. constructor tpointerconstnode.create(v : tpointerord;const t:ttype);
  313. begin
  314. inherited create(pointerconstn);
  315. value:=v;
  316. restype:=t;
  317. end;
  318. function tpointerconstnode.getcopy : tnode;
  319. var
  320. n : tpointerconstnode;
  321. begin
  322. n:=tpointerconstnode(inherited getcopy);
  323. n.value:=value;
  324. getcopy:=n;
  325. end;
  326. function tpointerconstnode.det_resulttype:tnode;
  327. begin
  328. result:=nil;
  329. resulttype:=restype;
  330. end;
  331. function tpointerconstnode.pass_1 : tnode;
  332. begin
  333. result:=nil;
  334. location.loc:=LOC_MEM;
  335. end;
  336. function tpointerconstnode.docompare(p: tnode): boolean;
  337. begin
  338. docompare :=
  339. inherited docompare(p) and
  340. (value = tpointerconstnode(p).value);
  341. end;
  342. {*****************************************************************************
  343. TSTRINGCONSTNODE
  344. *****************************************************************************}
  345. constructor tstringconstnode.createstr(const s : string;st:tstringtype);
  346. var
  347. l : longint;
  348. begin
  349. inherited create(stringconstn);
  350. l:=length(s);
  351. len:=l;
  352. { stringdup write even past a #0 }
  353. getmem(value_str,l+1);
  354. move(s[1],value_str^,l);
  355. value_str[l]:=#0;
  356. lab_str:=nil;
  357. if st=st_default then
  358. begin
  359. if cs_ansistrings in aktlocalswitches then
  360. st_type:=st_ansistring
  361. else
  362. st_type:=st_shortstring;
  363. end
  364. else
  365. st_type:=st;
  366. end;
  367. constructor tstringconstnode.createwstr(const w : tcompilerwidestring);
  368. begin
  369. inherited create(stringconstn);
  370. len:=getlengthwidestring(w);
  371. new(pcompilerwidestring(value_str));
  372. initwidestring(pcompilerwidestring(value_str)^);
  373. copywidestring(w,pcompilerwidestring(value_str)^);
  374. lab_str:=nil;
  375. st_type:=st_widestring;
  376. end;
  377. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  378. begin
  379. inherited create(stringconstn);
  380. len:=l;
  381. value_str:=s;
  382. if (cs_ansistrings in aktlocalswitches) or
  383. (len>255) then
  384. st_type:=st_ansistring
  385. else
  386. st_type:=st_shortstring;
  387. lab_str:=nil;
  388. end;
  389. destructor tstringconstnode.destroy;
  390. begin
  391. ansistringdispose(value_str,len);
  392. inherited destroy;
  393. end;
  394. function tstringconstnode.getcopy : tnode;
  395. var
  396. n : tstringconstnode;
  397. begin
  398. n:=tstringconstnode(inherited getcopy);
  399. n.st_type:=st_type;
  400. n.len:=len;
  401. n.lab_str:=lab_str;
  402. if st_type=st_widestring then
  403. copywidestring(pcompilerwidestring(value_str)^,pcompilerwidestring(n.value_str)^)
  404. else
  405. n.value_str:=getpcharcopy;
  406. getcopy:=n;
  407. end;
  408. function tstringconstnode.det_resulttype:tnode;
  409. begin
  410. result:=nil;
  411. case st_type of
  412. st_shortstring :
  413. resulttype:=cshortstringtype;
  414. st_ansistring :
  415. resulttype:=cansistringtype;
  416. st_widestring :
  417. resulttype:=cwidestringtype;
  418. st_longstring :
  419. resulttype:=clongstringtype;
  420. end;
  421. end;
  422. function tstringconstnode.pass_1 : tnode;
  423. begin
  424. result:=nil;
  425. location.loc:=LOC_MEM;
  426. end;
  427. function tstringconstnode.getpcharcopy : pchar;
  428. var
  429. pc : pchar;
  430. begin
  431. pc:=nil;
  432. getmem(pc,len+1);
  433. if pc=nil then
  434. Message(general_f_no_memory_left);
  435. move(value_str^,pc^,len+1);
  436. getpcharcopy:=pc;
  437. end;
  438. function tstringconstnode.docompare(p: tnode): boolean;
  439. begin
  440. docompare :=
  441. inherited docompare(p) and
  442. (len = tstringconstnode(p).len) and
  443. { Don't compare the pchars, since they may contain null chars }
  444. { Since all equal constant strings are replaced by the same }
  445. { label, the following compare should be enough (JM) }
  446. (lab_str = tstringconstnode(p).lab_str);
  447. end;
  448. {*****************************************************************************
  449. TSETCONSTNODE
  450. *****************************************************************************}
  451. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  452. begin
  453. inherited create(setconstn,nil);
  454. restype:=t;
  455. if assigned(s) then
  456. begin
  457. new(value_set);
  458. value_set^:=s^;
  459. end
  460. else
  461. value_set:=nil;
  462. end;
  463. destructor tsetconstnode.destroy;
  464. begin
  465. if assigned(value_set) then
  466. dispose(value_set);
  467. inherited destroy;
  468. end;
  469. function tsetconstnode.getcopy : tnode;
  470. var
  471. n : tsetconstnode;
  472. begin
  473. n:=tsetconstnode(inherited getcopy);
  474. if assigned(value_set) then
  475. begin
  476. new(n.value_set);
  477. n.value_set^:=value_set^
  478. end
  479. else
  480. n.value_set:=nil;
  481. n.lab_set:=lab_set;
  482. getcopy:=n;
  483. end;
  484. function tsetconstnode.det_resulttype:tnode;
  485. begin
  486. result:=nil;
  487. resulttype:=restype;
  488. end;
  489. function tsetconstnode.pass_1 : tnode;
  490. begin
  491. result:=nil;
  492. location.loc:=LOC_MEM;
  493. end;
  494. function tsetconstnode.docompare(p: tnode): boolean;
  495. var
  496. i: 0..31;
  497. begin
  498. if inherited docompare(p) then
  499. begin
  500. for i := 0 to 31 do
  501. if (value_set^[i] <> tsetconstnode(p).value_set^[i]) then
  502. begin
  503. docompare := false;
  504. exit
  505. end;
  506. docompare := true;
  507. end
  508. else
  509. docompare := false;
  510. end;
  511. {*****************************************************************************
  512. TNILNODE
  513. *****************************************************************************}
  514. constructor tnilnode.create;
  515. begin
  516. inherited create(niln);
  517. end;
  518. function tnilnode.det_resulttype:tnode;
  519. begin
  520. result:=nil;
  521. resulttype:=voidpointertype;
  522. end;
  523. function tnilnode.pass_1 : tnode;
  524. begin
  525. result:=nil;
  526. location.loc:=LOC_MEM;
  527. end;
  528. begin
  529. crealconstnode:=trealconstnode;
  530. cordconstnode:=tordconstnode;
  531. cpointerconstnode:=tpointerconstnode;
  532. cstringconstnode:=tstringconstnode;
  533. csetconstnode:=tsetconstnode;
  534. cnilnode:=tnilnode;
  535. end.
  536. {
  537. $Log$
  538. Revision 1.17 2001-04-13 01:22:09 peter
  539. * symtable change to classes
  540. * range check generation and errors fixed, make cycle DEBUG=1 works
  541. * memory leaks fixed
  542. Revision 1.16 2001/04/02 21:20:30 peter
  543. * resulttype rewrite
  544. Revision 1.15 2000/12/31 11:14:10 jonas
  545. + implemented/fixed docompare() mathods for all nodes (not tested)
  546. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  547. and constant strings/chars together
  548. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  549. when adding
  550. Revision 1.14 2000/12/16 15:58:48 jonas
  551. * genintconstnode now returns cardinals instead of int64 constants if possible
  552. Revision 1.13 2000/12/15 13:26:01 jonas
  553. * only return int64's from functions if it int64funcresok is defined
  554. + added int64funcresok define to options.pas
  555. Revision 1.12 2000/12/07 17:19:42 jonas
  556. * new constant handling: from now on, hex constants >$7fffffff are
  557. parsed as unsigned constants (otherwise, $80000000 got sign extended
  558. and became $ffffffff80000000), all constants in the longint range
  559. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  560. are cardinals and the rest are int64's.
  561. * added lots of longint typecast to prevent range check errors in the
  562. compiler and rtl
  563. * type casts of symbolic ordinal constants are now preserved
  564. * fixed bug where the original resulttype.def wasn't restored correctly
  565. after doing a 64bit rangecheck
  566. Revision 1.11 2000/11/29 00:30:32 florian
  567. * unused units removed from uses clause
  568. * some changes for widestrings
  569. Revision 1.10 2000/10/31 22:02:48 peter
  570. * symtable splitted, no real code changes
  571. Revision 1.9 2000/10/14 21:52:55 peter
  572. * fixed memory leaks
  573. Revision 1.8 2000/10/14 10:14:50 peter
  574. * moehrendorf oct 2000 rewrite
  575. Revision 1.7 2000/09/28 19:49:52 florian
  576. *** empty log message ***
  577. Revision 1.6 2000/09/27 20:25:44 florian
  578. * more stuff fixed
  579. Revision 1.5 2000/09/27 18:14:31 florian
  580. * fixed a lot of syntax errors in the n*.pas stuff
  581. Revision 1.4 2000/09/26 14:59:34 florian
  582. * more conversion work done
  583. Revision 1.3 2000/09/24 21:15:34 florian
  584. * some errors fix to get more stuff compilable
  585. Revision 1.2 2000/09/24 15:06:19 peter
  586. * use defines.inc
  587. Revision 1.1 2000/09/22 21:44:48 florian
  588. + initial revision
  589. }