ncon.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. n.restype := restype;
  296. getcopy:=n;
  297. end;
  298. function tordconstnode.det_resulttype:tnode;
  299. begin
  300. result:=nil;
  301. resulttype:=restype;
  302. testrange(resulttype.def,value,false);
  303. end;
  304. function tordconstnode.pass_1 : tnode;
  305. begin
  306. result:=nil;
  307. location.loc:=LOC_MEM;
  308. end;
  309. function tordconstnode.docompare(p: tnode): boolean;
  310. begin
  311. docompare :=
  312. inherited docompare(p) and
  313. (value = tordconstnode(p).value);
  314. end;
  315. {*****************************************************************************
  316. TPOINTERCONSTNODE
  317. *****************************************************************************}
  318. constructor tpointerconstnode.create(v : tpointerord;const t:ttype);
  319. begin
  320. inherited create(pointerconstn);
  321. value:=v;
  322. restype:=t;
  323. end;
  324. function tpointerconstnode.getcopy : tnode;
  325. var
  326. n : tpointerconstnode;
  327. begin
  328. n:=tpointerconstnode(inherited getcopy);
  329. n.value:=value;
  330. n.restype := restype;
  331. getcopy:=n;
  332. end;
  333. function tpointerconstnode.det_resulttype:tnode;
  334. begin
  335. result:=nil;
  336. resulttype:=restype;
  337. end;
  338. function tpointerconstnode.pass_1 : tnode;
  339. begin
  340. result:=nil;
  341. location.loc:=LOC_MEM;
  342. end;
  343. function tpointerconstnode.docompare(p: tnode): boolean;
  344. begin
  345. docompare :=
  346. inherited docompare(p) and
  347. (value = tpointerconstnode(p).value);
  348. end;
  349. {*****************************************************************************
  350. TSTRINGCONSTNODE
  351. *****************************************************************************}
  352. constructor tstringconstnode.createstr(const s : string;st:tstringtype);
  353. var
  354. l : longint;
  355. begin
  356. inherited create(stringconstn);
  357. l:=length(s);
  358. len:=l;
  359. { stringdup write even past a #0 }
  360. getmem(value_str,l+1);
  361. move(s[1],value_str^,l);
  362. value_str[l]:=#0;
  363. lab_str:=nil;
  364. if st=st_default then
  365. begin
  366. if cs_ansistrings in aktlocalswitches then
  367. st_type:=st_ansistring
  368. else
  369. st_type:=st_shortstring;
  370. end
  371. else
  372. st_type:=st;
  373. end;
  374. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  375. begin
  376. inherited create(stringconstn);
  377. len:=getlengthwidestring(w);
  378. initwidestring(pcompilerwidestring(value_str));
  379. copywidestring(w,pcompilerwidestring(value_str));
  380. lab_str:=nil;
  381. st_type:=st_widestring;
  382. end;
  383. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  384. begin
  385. inherited create(stringconstn);
  386. len:=l;
  387. value_str:=s;
  388. if (cs_ansistrings in aktlocalswitches) or
  389. (len>255) then
  390. st_type:=st_ansistring
  391. else
  392. st_type:=st_shortstring;
  393. lab_str:=nil;
  394. end;
  395. destructor tstringconstnode.destroy;
  396. begin
  397. if st_type=st_widestring then
  398. donewidestring(pcompilerwidestring(value_str))
  399. else
  400. ansistringdispose(value_str,len);
  401. inherited destroy;
  402. end;
  403. function tstringconstnode.getcopy : tnode;
  404. var
  405. n : tstringconstnode;
  406. begin
  407. n:=tstringconstnode(inherited getcopy);
  408. n.st_type:=st_type;
  409. n.len:=len;
  410. n.lab_str:=lab_str;
  411. if st_type=st_widestring then
  412. begin
  413. initwidestring(pcompilerwidestring(n.value_str));
  414. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  415. end
  416. else
  417. n.value_str:=getpcharcopy;
  418. getcopy:=n;
  419. end;
  420. function tstringconstnode.det_resulttype:tnode;
  421. begin
  422. result:=nil;
  423. case st_type of
  424. st_shortstring :
  425. resulttype:=cshortstringtype;
  426. st_ansistring :
  427. resulttype:=cansistringtype;
  428. st_widestring :
  429. resulttype:=cwidestringtype;
  430. st_longstring :
  431. resulttype:=clongstringtype;
  432. end;
  433. end;
  434. function tstringconstnode.pass_1 : tnode;
  435. begin
  436. result:=nil;
  437. location.loc:=LOC_MEM;
  438. end;
  439. function tstringconstnode.getpcharcopy : pchar;
  440. var
  441. pc : pchar;
  442. begin
  443. pc:=nil;
  444. getmem(pc,len+1);
  445. if pc=nil then
  446. Message(general_f_no_memory_left);
  447. move(value_str^,pc^,len+1);
  448. getpcharcopy:=pc;
  449. end;
  450. function tstringconstnode.docompare(p: tnode): boolean;
  451. begin
  452. docompare :=
  453. inherited docompare(p) and
  454. (len = tstringconstnode(p).len) and
  455. { Don't compare the pchars, since they may contain null chars }
  456. { Since all equal constant strings are replaced by the same }
  457. { label, the following compare should be enough (JM) }
  458. (lab_str = tstringconstnode(p).lab_str);
  459. end;
  460. {*****************************************************************************
  461. TSETCONSTNODE
  462. *****************************************************************************}
  463. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  464. begin
  465. inherited create(setconstn,nil);
  466. restype:=t;
  467. if assigned(s) then
  468. begin
  469. new(value_set);
  470. value_set^:=s^;
  471. end
  472. else
  473. value_set:=nil;
  474. end;
  475. destructor tsetconstnode.destroy;
  476. begin
  477. if assigned(value_set) then
  478. dispose(value_set);
  479. inherited destroy;
  480. end;
  481. function tsetconstnode.getcopy : tnode;
  482. var
  483. n : tsetconstnode;
  484. begin
  485. n:=tsetconstnode(inherited getcopy);
  486. if assigned(value_set) then
  487. begin
  488. new(n.value_set);
  489. n.value_set^:=value_set^
  490. end
  491. else
  492. n.value_set:=nil;
  493. n.restype := restype;
  494. n.lab_set:=lab_set;
  495. getcopy:=n;
  496. end;
  497. function tsetconstnode.det_resulttype:tnode;
  498. begin
  499. result:=nil;
  500. resulttype:=restype;
  501. end;
  502. function tsetconstnode.pass_1 : tnode;
  503. begin
  504. result:=nil;
  505. location.loc:=LOC_MEM;
  506. end;
  507. function tsetconstnode.docompare(p: tnode): boolean;
  508. var
  509. i: 0..31;
  510. begin
  511. if inherited docompare(p) then
  512. begin
  513. for i := 0 to 31 do
  514. if (value_set^[i] <> tsetconstnode(p).value_set^[i]) then
  515. begin
  516. docompare := false;
  517. exit
  518. end;
  519. docompare := true;
  520. end
  521. else
  522. docompare := false;
  523. end;
  524. {*****************************************************************************
  525. TNILNODE
  526. *****************************************************************************}
  527. constructor tnilnode.create;
  528. begin
  529. inherited create(niln);
  530. end;
  531. function tnilnode.det_resulttype:tnode;
  532. begin
  533. result:=nil;
  534. resulttype:=voidpointertype;
  535. end;
  536. function tnilnode.pass_1 : tnode;
  537. begin
  538. result:=nil;
  539. location.loc:=LOC_MEM;
  540. end;
  541. begin
  542. crealconstnode:=trealconstnode;
  543. cordconstnode:=tordconstnode;
  544. cpointerconstnode:=tpointerconstnode;
  545. cstringconstnode:=tstringconstnode;
  546. csetconstnode:=tsetconstnode;
  547. cnilnode:=tnilnode;
  548. end.
  549. {
  550. $Log$
  551. Revision 1.20 2001-08-06 10:18:39 jonas
  552. * restype wasn't copied for some constant nodetypes in getcopy
  553. Revision 1.19 2001/07/08 21:00:15 peter
  554. * various widestring updates, it works now mostly without charset
  555. mapping supported
  556. Revision 1.18 2001/05/08 21:06:30 florian
  557. * some more support for widechars commited especially
  558. regarding type casting and constants
  559. Revision 1.17 2001/04/13 01:22:09 peter
  560. * symtable change to classes
  561. * range check generation and errors fixed, make cycle DEBUG=1 works
  562. * memory leaks fixed
  563. Revision 1.16 2001/04/02 21:20:30 peter
  564. * resulttype rewrite
  565. Revision 1.15 2000/12/31 11:14:10 jonas
  566. + implemented/fixed docompare() mathods for all nodes (not tested)
  567. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  568. and constant strings/chars together
  569. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  570. when adding
  571. Revision 1.14 2000/12/16 15:58:48 jonas
  572. * genintconstnode now returns cardinals instead of int64 constants if possible
  573. Revision 1.13 2000/12/15 13:26:01 jonas
  574. * only return int64's from functions if it int64funcresok is defined
  575. + added int64funcresok define to options.pas
  576. Revision 1.12 2000/12/07 17:19:42 jonas
  577. * new constant handling: from now on, hex constants >$7fffffff are
  578. parsed as unsigned constants (otherwise, $80000000 got sign extended
  579. and became $ffffffff80000000), all constants in the longint range
  580. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  581. are cardinals and the rest are int64's.
  582. * added lots of longint typecast to prevent range check errors in the
  583. compiler and rtl
  584. * type casts of symbolic ordinal constants are now preserved
  585. * fixed bug where the original resulttype.def wasn't restored correctly
  586. after doing a 64bit rangecheck
  587. Revision 1.11 2000/11/29 00:30:32 florian
  588. * unused units removed from uses clause
  589. * some changes for widestrings
  590. Revision 1.10 2000/10/31 22:02:48 peter
  591. * symtable splitted, no real code changes
  592. Revision 1.9 2000/10/14 21:52:55 peter
  593. * fixed memory leaks
  594. Revision 1.8 2000/10/14 10:14:50 peter
  595. * moehrendorf oct 2000 rewrite
  596. Revision 1.7 2000/09/28 19:49:52 florian
  597. *** empty log message ***
  598. Revision 1.6 2000/09/27 20:25:44 florian
  599. * more stuff fixed
  600. Revision 1.5 2000/09/27 18:14:31 florian
  601. * fixed a lot of syntax errors in the n*.pas stuff
  602. Revision 1.4 2000/09/26 14:59:34 florian
  603. * more conversion work done
  604. Revision 1.3 2000/09/24 21:15:34 florian
  605. * some errors fix to get more stuff compilable
  606. Revision 1.2 2000/09/24 15:06:19 peter
  607. * use defines.inc
  608. Revision 1.1 2000/09/22 21:44:48 florian
  609. + initial revision
  610. }