ncon.pas 18 KB

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