ncon.pas 20 KB

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