ncon.pas 21 KB

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