ncon.pas 20 KB

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