ncon.pas 20 KB

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