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. { generic code }
  243. { overridden by: }
  244. { i386 }
  245. constructor trealconstnode.create(v : bestreal;const t:ttype);
  246. begin
  247. inherited create(realconstn);
  248. restype:=t;
  249. value_real:=v;
  250. lab_real:=nil;
  251. end;
  252. function trealconstnode.getcopy : tnode;
  253. var
  254. n : trealconstnode;
  255. begin
  256. n:=trealconstnode(inherited getcopy);
  257. n.value_real:=value_real;
  258. n.lab_real:=lab_real;
  259. getcopy:=n;
  260. end;
  261. function trealconstnode.det_resulttype:tnode;
  262. begin
  263. result:=nil;
  264. resulttype:=restype;
  265. end;
  266. function trealconstnode.pass_1 : tnode;
  267. begin
  268. result:=nil;
  269. location.loc:=LOC_MEM;
  270. end;
  271. function trealconstnode.docompare(p: tnode): boolean;
  272. begin
  273. docompare :=
  274. inherited docompare(p) and
  275. (value_real = trealconstnode(p).value_real);
  276. end;
  277. {*****************************************************************************
  278. TORDCONSTNODE
  279. *****************************************************************************}
  280. constructor tordconstnode.create(v : tconstexprint;const t:ttype);
  281. begin
  282. inherited create(ordconstn);
  283. value:=v;
  284. restype:=t;
  285. end;
  286. function tordconstnode.getcopy : tnode;
  287. var
  288. n : tordconstnode;
  289. begin
  290. n:=tordconstnode(inherited getcopy);
  291. n.value:=value;
  292. n.restype := restype;
  293. getcopy:=n;
  294. end;
  295. function tordconstnode.det_resulttype:tnode;
  296. begin
  297. result:=nil;
  298. resulttype:=restype;
  299. testrange(resulttype.def,value,false);
  300. end;
  301. function tordconstnode.pass_1 : tnode;
  302. begin
  303. result:=nil;
  304. location.loc:=LOC_MEM;
  305. end;
  306. function tordconstnode.docompare(p: tnode): boolean;
  307. begin
  308. docompare :=
  309. inherited docompare(p) and
  310. (value = tordconstnode(p).value);
  311. end;
  312. {*****************************************************************************
  313. TPOINTERCONSTNODE
  314. *****************************************************************************}
  315. constructor tpointerconstnode.create(v : tpointerord;const t:ttype);
  316. begin
  317. inherited create(pointerconstn);
  318. value:=v;
  319. restype:=t;
  320. end;
  321. function tpointerconstnode.getcopy : tnode;
  322. var
  323. n : tpointerconstnode;
  324. begin
  325. n:=tpointerconstnode(inherited getcopy);
  326. n.value:=value;
  327. n.restype := restype;
  328. getcopy:=n;
  329. end;
  330. function tpointerconstnode.det_resulttype:tnode;
  331. begin
  332. result:=nil;
  333. resulttype:=restype;
  334. end;
  335. function tpointerconstnode.pass_1 : tnode;
  336. begin
  337. result:=nil;
  338. location.loc:=LOC_MEM;
  339. end;
  340. function tpointerconstnode.docompare(p: tnode): boolean;
  341. begin
  342. docompare :=
  343. inherited docompare(p) and
  344. (value = tpointerconstnode(p).value);
  345. end;
  346. {*****************************************************************************
  347. TSTRINGCONSTNODE
  348. *****************************************************************************}
  349. constructor tstringconstnode.createstr(const s : string;st:tstringtype);
  350. var
  351. l : longint;
  352. begin
  353. inherited create(stringconstn);
  354. l:=length(s);
  355. len:=l;
  356. { stringdup write even past a #0 }
  357. getmem(value_str,l+1);
  358. move(s[1],value_str^,l);
  359. value_str[l]:=#0;
  360. lab_str:=nil;
  361. if st=st_default then
  362. begin
  363. if cs_ansistrings in aktlocalswitches then
  364. st_type:=st_ansistring
  365. else
  366. st_type:=st_shortstring;
  367. end
  368. else
  369. st_type:=st;
  370. end;
  371. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  372. begin
  373. inherited create(stringconstn);
  374. len:=getlengthwidestring(w);
  375. initwidestring(pcompilerwidestring(value_str));
  376. copywidestring(w,pcompilerwidestring(value_str));
  377. lab_str:=nil;
  378. st_type:=st_widestring;
  379. end;
  380. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  381. begin
  382. inherited create(stringconstn);
  383. len:=l;
  384. value_str:=s;
  385. if (cs_ansistrings in aktlocalswitches) or
  386. (len>255) then
  387. st_type:=st_ansistring
  388. else
  389. st_type:=st_shortstring;
  390. lab_str:=nil;
  391. end;
  392. destructor tstringconstnode.destroy;
  393. begin
  394. if st_type=st_widestring then
  395. donewidestring(pcompilerwidestring(value_str))
  396. else
  397. ansistringdispose(value_str,len);
  398. inherited destroy;
  399. end;
  400. function tstringconstnode.getcopy : tnode;
  401. var
  402. n : tstringconstnode;
  403. begin
  404. n:=tstringconstnode(inherited getcopy);
  405. n.st_type:=st_type;
  406. n.len:=len;
  407. n.lab_str:=lab_str;
  408. if st_type=st_widestring then
  409. begin
  410. initwidestring(pcompilerwidestring(n.value_str));
  411. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  412. end
  413. else
  414. n.value_str:=getpcharcopy;
  415. getcopy:=n;
  416. end;
  417. function tstringconstnode.det_resulttype:tnode;
  418. begin
  419. result:=nil;
  420. case st_type of
  421. st_shortstring :
  422. resulttype:=cshortstringtype;
  423. st_ansistring :
  424. resulttype:=cansistringtype;
  425. st_widestring :
  426. resulttype:=cwidestringtype;
  427. st_longstring :
  428. resulttype:=clongstringtype;
  429. end;
  430. end;
  431. function tstringconstnode.pass_1 : tnode;
  432. begin
  433. result:=nil;
  434. location.loc:=LOC_MEM;
  435. end;
  436. function tstringconstnode.getpcharcopy : pchar;
  437. var
  438. pc : pchar;
  439. begin
  440. pc:=nil;
  441. getmem(pc,len+1);
  442. if pc=nil then
  443. Message(general_f_no_memory_left);
  444. move(value_str^,pc^,len+1);
  445. getpcharcopy:=pc;
  446. end;
  447. function tstringconstnode.docompare(p: tnode): boolean;
  448. begin
  449. docompare :=
  450. inherited docompare(p) and
  451. (len = tstringconstnode(p).len) and
  452. { Don't compare the pchars, since they may contain null chars }
  453. { Since all equal constant strings are replaced by the same }
  454. { label, the following compare should be enough (JM) }
  455. (lab_str = tstringconstnode(p).lab_str);
  456. end;
  457. {*****************************************************************************
  458. TSETCONSTNODE
  459. *****************************************************************************}
  460. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  461. begin
  462. inherited create(setconstn,nil);
  463. restype:=t;
  464. if assigned(s) then
  465. begin
  466. new(value_set);
  467. value_set^:=s^;
  468. end
  469. else
  470. value_set:=nil;
  471. end;
  472. destructor tsetconstnode.destroy;
  473. begin
  474. if assigned(value_set) then
  475. dispose(value_set);
  476. inherited destroy;
  477. end;
  478. function tsetconstnode.getcopy : tnode;
  479. var
  480. n : tsetconstnode;
  481. begin
  482. n:=tsetconstnode(inherited getcopy);
  483. if assigned(value_set) then
  484. begin
  485. new(n.value_set);
  486. n.value_set^:=value_set^
  487. end
  488. else
  489. n.value_set:=nil;
  490. n.restype := restype;
  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.21 2001-08-26 13:36:40 florian
  549. * some cg reorganisation
  550. * some PPC updates
  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. }