ncon.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for constants
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,widestr,constexp,
  22. cclasses,
  23. node,
  24. aasmbase,aasmtai,aasmdata,cpuinfo,globals,
  25. symconst,symtype,symdef,symsym;
  26. type
  27. tdataconstnode = class(tnode)
  28. data : tdynamicarray;
  29. maxalign : word;
  30. constructor create;virtual;
  31. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  32. destructor destroy;override;
  33. procedure ppuwrite(ppufile:tcompilerppufile);override;
  34. function dogetcopy : tnode;override;
  35. function pass_1 : tnode;override;
  36. function pass_typecheck:tnode;override;
  37. function docompare(p: tnode) : boolean; override;
  38. procedure printnodedata(var t:text);override;
  39. procedure append(const d;len : aint);
  40. procedure align(value : word);
  41. end;
  42. tdataconstnodeclass = class of tdataconstnode;
  43. trealconstnode = class(tnode)
  44. typedef : tdef;
  45. typedefderef : tderef;
  46. value_real : bestreal;
  47. value_currency : currency;
  48. lab_real : tasmlabel;
  49. constructor create(v : bestreal;def:tdef);virtual;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure buildderefimpl;override;
  53. procedure derefimpl;override;
  54. function dogetcopy : tnode;override;
  55. function pass_1 : tnode;override;
  56. function pass_typecheck:tnode;override;
  57. function docompare(p: tnode) : boolean; override;
  58. procedure printnodedata(var t:text);override;
  59. end;
  60. trealconstnodeclass = class of trealconstnode;
  61. tordconstnode = class(tnode)
  62. typedef : tdef;
  63. typedefderef : tderef;
  64. value : TConstExprInt;
  65. rangecheck : boolean;
  66. { create an ordinal constant node of the specified type and value.
  67. _rangecheck determines if the value of the ordinal should be checked
  68. against the ranges of the type definition.
  69. }
  70. constructor create(v : tconstexprint;def:tdef; _rangecheck : boolean);virtual;
  71. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  72. procedure ppuwrite(ppufile:tcompilerppufile);override;
  73. procedure buildderefimpl;override;
  74. procedure derefimpl;override;
  75. function dogetcopy : tnode;override;
  76. function pass_1 : tnode;override;
  77. function pass_typecheck:tnode;override;
  78. function docompare(p: tnode) : boolean; override;
  79. procedure printnodedata(var t:text);override;
  80. end;
  81. tordconstnodeclass = class of tordconstnode;
  82. tpointerconstnode = class(tnode)
  83. typedef : tdef;
  84. typedefderef : tderef;
  85. value : TConstPtrUInt;
  86. constructor create(v : TConstPtrUInt;def:tdef);virtual;
  87. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderefimpl;override;
  90. procedure derefimpl;override;
  91. function dogetcopy : tnode;override;
  92. function pass_1 : tnode;override;
  93. function pass_typecheck:tnode;override;
  94. function docompare(p: tnode) : boolean; override;
  95. end;
  96. tpointerconstnodeclass = class of tpointerconstnode;
  97. tconststringtype = (
  98. cst_conststring,
  99. cst_shortstring,
  100. cst_longstring,
  101. cst_ansistring,
  102. cst_widestring,
  103. cst_unicodestring
  104. );
  105. tstringconstnode = class(tnode)
  106. value_str : pchar;
  107. len : longint;
  108. lab_str : tasmlabel;
  109. cst_type : tconststringtype;
  110. constructor createstr(const s : string);virtual;
  111. constructor createpchar(s : pchar;l : longint);virtual;
  112. constructor createwstr(w : pcompilerwidestring);virtual;
  113. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  114. procedure ppuwrite(ppufile:tcompilerppufile);override;
  115. procedure buildderefimpl;override;
  116. procedure derefimpl;override;
  117. destructor destroy;override;
  118. function dogetcopy : tnode;override;
  119. function pass_1 : tnode;override;
  120. function pass_typecheck:tnode;override;
  121. function getpcharcopy : pchar;
  122. function docompare(p: tnode) : boolean; override;
  123. procedure changestringtype(def:tdef);
  124. end;
  125. tstringconstnodeclass = class of tstringconstnode;
  126. tsetconstnode = class(tunarynode)
  127. typedef : tdef;
  128. typedefderef : tderef;
  129. value_set : pconstset;
  130. lab_set : tasmlabel;
  131. constructor create(s : pconstset;def:tdef);virtual;
  132. destructor destroy;override;
  133. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  134. procedure ppuwrite(ppufile:tcompilerppufile);override;
  135. procedure buildderefimpl;override;
  136. procedure derefimpl;override;
  137. procedure adjustforsetbase;
  138. function dogetcopy : tnode;override;
  139. function pass_1 : tnode;override;
  140. function pass_typecheck:tnode;override;
  141. function docompare(p: tnode) : boolean; override;
  142. end;
  143. tsetconstnodeclass = class of tsetconstnode;
  144. tnilnode = class(tnode)
  145. constructor create;virtual;
  146. function pass_1 : tnode;override;
  147. function pass_typecheck:tnode;override;
  148. end;
  149. tnilnodeclass = class of tnilnode;
  150. tguidconstnode = class(tnode)
  151. value : tguid;
  152. constructor create(const g:tguid);virtual;
  153. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;
  155. function dogetcopy : tnode;override;
  156. function pass_1 : tnode;override;
  157. function pass_typecheck:tnode;override;
  158. function docompare(p: tnode) : boolean; override;
  159. end;
  160. tguidconstnodeclass = class of tguidconstnode;
  161. var
  162. cdataconstnode : tdataconstnodeclass;
  163. crealconstnode : trealconstnodeclass;
  164. cordconstnode : tordconstnodeclass;
  165. cpointerconstnode : tpointerconstnodeclass;
  166. cstringconstnode : tstringconstnodeclass;
  167. csetconstnode : tsetconstnodeclass;
  168. cguidconstnode : tguidconstnodeclass;
  169. cnilnode : tnilnodeclass;
  170. function genintconstnode(v : TConstExprInt) : tordconstnode;
  171. function genenumnode(v : tenumsym) : tordconstnode;
  172. { some helper routines }
  173. function get_ordinal_value(p : tnode) : TConstExprInt;
  174. function is_constresourcestringnode(p : tnode) : boolean;
  175. function is_emptyset(p : tnode):boolean;
  176. function genconstsymtree(p : tconstsym) : tnode;
  177. implementation
  178. uses
  179. cutils,
  180. verbose,systems,
  181. defutil,
  182. cpubase,cgbase,
  183. nld;
  184. function genintconstnode(v : TConstExprInt) : tordconstnode;
  185. var
  186. htype : tdef;
  187. begin
  188. int_to_type(v,htype);
  189. genintconstnode:=cordconstnode.create(v,htype,true);
  190. end;
  191. function genenumnode(v : tenumsym) : tordconstnode;
  192. var
  193. htype : tdef;
  194. begin
  195. htype:=v.definition;
  196. genenumnode:=cordconstnode.create(int64(v.value),htype,true);
  197. end;
  198. function get_ordinal_value(p : tnode) : TConstExprInt;
  199. begin
  200. get_ordinal_value:=0;
  201. if is_constnode(p) then
  202. begin
  203. if p.nodetype=ordconstn then
  204. get_ordinal_value:=tordconstnode(p).value
  205. else
  206. Message(type_e_ordinal_expr_expected);
  207. end
  208. else
  209. Message(type_e_constant_expr_expected);
  210. end;
  211. function is_constresourcestringnode(p : tnode) : boolean;
  212. begin
  213. is_constresourcestringnode:=(p.nodetype=loadn) and
  214. (tloadnode(p).symtableentry.typ=constsym) and
  215. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  216. end;
  217. function is_emptyset(p : tnode):boolean;
  218. begin
  219. is_emptyset:=(p.nodetype=setconstn) and
  220. (Tsetconstnode(p).value_set^=[]);
  221. end;
  222. function genconstsymtree(p : tconstsym) : tnode;
  223. var
  224. p1 : tnode;
  225. len : longint;
  226. pc : pchar;
  227. begin
  228. p1:=nil;
  229. case p.consttyp of
  230. constord :
  231. p1:=cordconstnode.create(p.value.valueord,p.constdef,true);
  232. conststring :
  233. begin
  234. len:=p.value.len;
  235. getmem(pc,len+1);
  236. move(pchar(p.value.valueptr)^,pc^,len);
  237. pc[len]:=#0;
  238. p1:=cstringconstnode.createpchar(pc,len);
  239. end;
  240. constreal :
  241. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
  242. constset :
  243. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.constdef);
  244. constpointer :
  245. p1:=cpointerconstnode.create(p.value.valueordptr,p.constdef);
  246. constnil :
  247. p1:=cnilnode.create;
  248. else
  249. internalerror(200205103);
  250. end;
  251. genconstsymtree:=p1;
  252. end;
  253. {*****************************************************************************
  254. TDATACONSTNODE
  255. *****************************************************************************}
  256. constructor tdataconstnode.create;
  257. begin
  258. inherited create(dataconstn);
  259. data:=tdynamicarray.create(128);
  260. end;
  261. constructor tdataconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  262. var
  263. len : aint;
  264. buf : array[0..255] of byte;
  265. begin
  266. inherited ppuload(t,ppufile);
  267. len:=ppufile.getaint;
  268. if len<4096 then
  269. data:=tdynamicarray.create(len)
  270. else
  271. data:=tdynamicarray.create(4096);
  272. while len>0 do
  273. begin
  274. if len>sizeof(buf) then
  275. begin
  276. ppufile.getdata(buf,sizeof(buf));
  277. data.write(buf,sizeof(buf));
  278. dec(len,sizeof(buf));
  279. end
  280. else
  281. begin
  282. ppufile.getdata(buf,len);
  283. data.write(buf,len);
  284. len:=0;
  285. end;
  286. end;
  287. end;
  288. destructor tdataconstnode.destroy;
  289. begin
  290. data.free;
  291. inherited destroy;
  292. end;
  293. procedure tdataconstnode.ppuwrite(ppufile:tcompilerppufile);
  294. var
  295. len : aint;
  296. buf : array[0..255] of byte;
  297. begin
  298. inherited ppuwrite(ppufile);
  299. len:=data.size;
  300. ppufile.putaint(len);
  301. data.seek(0);
  302. while len>0 do
  303. begin
  304. if len>sizeof(buf) then
  305. begin
  306. data.read(buf,sizeof(buf));
  307. ppufile.putdata(buf,sizeof(buf));
  308. dec(len,sizeof(buf));
  309. end
  310. else
  311. begin
  312. data.read(buf,len);
  313. ppufile.putdata(buf,len);
  314. len:=0;
  315. end;
  316. end;
  317. end;
  318. function tdataconstnode.dogetcopy : tnode;
  319. var
  320. n : tdataconstnode;
  321. len : aint;
  322. buf : array[0..255] of byte;
  323. begin
  324. n:=tdataconstnode(inherited dogetcopy);
  325. len:=data.size;
  326. if len<4096 then
  327. n.data:=tdynamicarray.create(len)
  328. else
  329. n.data:=tdynamicarray.create(4096);
  330. data.seek(0);
  331. while len>0 do
  332. begin
  333. if len>sizeof(buf) then
  334. begin
  335. data.read(buf,sizeof(buf));
  336. n.data.write(buf,sizeof(buf));
  337. dec(len,sizeof(buf));
  338. end
  339. else
  340. begin
  341. data.read(buf,len);
  342. n.data.write(buf,len);
  343. len:=0;
  344. end;
  345. end;
  346. dogetcopy := n;
  347. end;
  348. function tdataconstnode.pass_1 : tnode;
  349. begin
  350. result:=nil;
  351. expectloc:=LOC_CREFERENCE;
  352. end;
  353. function tdataconstnode.pass_typecheck:tnode;
  354. begin
  355. result:=nil;
  356. resultdef:=voidpointertype;
  357. end;
  358. function tdataconstnode.docompare(p: tnode) : boolean;
  359. var
  360. b1,b2 : byte;
  361. I : longint;
  362. begin
  363. docompare :=
  364. inherited docompare(p) and (data.size=tdataconstnode(p).data.size);
  365. if docompare then
  366. begin
  367. data.seek(0);
  368. tdataconstnode(p).data.seek(0);
  369. for i:=0 to data.size-1 do
  370. begin
  371. data.read(b1,1);
  372. tdataconstnode(p).data.read(b2,1);
  373. if b1<>b2 then
  374. begin
  375. docompare:=false;
  376. exit;
  377. end;
  378. end;
  379. end;
  380. end;
  381. procedure tdataconstnode.printnodedata(var t:text);
  382. var
  383. i : longint;
  384. b : byte;
  385. begin
  386. inherited printnodedata(t);
  387. write(t,printnodeindention,'data size = ',data.size,' data = ');
  388. data.seek(0);
  389. for i:=0 to data.size-1 do
  390. begin
  391. data.read(b,1);
  392. if i=data.size-1 then
  393. writeln(t,b)
  394. else
  395. write(t,b,',');
  396. end;
  397. end;
  398. procedure tdataconstnode.append(const d;len : aint);
  399. begin
  400. data.seek(data.size);
  401. data.write(d,len);
  402. end;
  403. procedure tdataconstnode.align(value : word);
  404. begin
  405. if value>maxalign then
  406. maxalign:=value;
  407. data.align(value);
  408. end;
  409. {*****************************************************************************
  410. TREALCONSTNODE
  411. *****************************************************************************}
  412. { generic code }
  413. { overridden by: }
  414. { i386 }
  415. constructor trealconstnode.create(v : bestreal;def:tdef);
  416. begin
  417. inherited create(realconstn);
  418. typedef:=def;
  419. value_real:=v;
  420. value_currency:=v;
  421. lab_real:=nil;
  422. end;
  423. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  424. var
  425. i : int64;
  426. begin
  427. inherited ppuload(t,ppufile);
  428. ppufile.getderef(typedefderef);
  429. value_real:=ppufile.getreal;
  430. i:=ppufile.getint64;
  431. value_currency:=PCurrency(@i)^;
  432. lab_real:=tasmlabel(ppufile.getasmsymbol);
  433. end;
  434. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  435. begin
  436. inherited ppuwrite(ppufile);
  437. ppufile.putderef(typedefderef);
  438. ppufile.putreal(value_real);
  439. ppufile.putint64(PInt64(@value_currency)^);
  440. ppufile.putasmsymbol(lab_real);
  441. end;
  442. procedure trealconstnode.buildderefimpl;
  443. begin
  444. inherited buildderefimpl;
  445. typedefderef.build(typedef);
  446. end;
  447. procedure trealconstnode.derefimpl;
  448. begin
  449. inherited derefimpl;
  450. typedef:=tdef(typedefderef.resolve);
  451. end;
  452. function trealconstnode.dogetcopy : tnode;
  453. var
  454. n : trealconstnode;
  455. begin
  456. n:=trealconstnode(inherited dogetcopy);
  457. n.value_real:=value_real;
  458. n.value_currency:=value_currency;
  459. n.lab_real:=lab_real;
  460. dogetcopy:=n;
  461. end;
  462. function trealconstnode.pass_typecheck:tnode;
  463. begin
  464. result:=nil;
  465. resultdef:=typedef;
  466. end;
  467. function trealconstnode.pass_1 : tnode;
  468. begin
  469. result:=nil;
  470. expectloc:=LOC_CREFERENCE;
  471. end;
  472. function trealconstnode.docompare(p: tnode): boolean;
  473. begin
  474. docompare :=
  475. inherited docompare(p) and
  476. (value_real = trealconstnode(p).value_real) and
  477. { floating point compares for non-numbers give strange results usually }
  478. is_number_float(value_real) and
  479. is_number_float(trealconstnode(p).value_real);
  480. end;
  481. procedure Trealconstnode.printnodedata(var t:text);
  482. begin
  483. inherited printnodedata(t);
  484. writeln(t,printnodeindention,'value = ',value_real);
  485. end;
  486. {*****************************************************************************
  487. TORDCONSTNODE
  488. *****************************************************************************}
  489. constructor tordconstnode.create(v : tconstexprint;def:tdef;_rangecheck : boolean);
  490. begin
  491. inherited create(ordconstn);
  492. value:=v;
  493. typedef:=def;
  494. rangecheck := _rangecheck;
  495. end;
  496. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  497. begin
  498. inherited ppuload(t,ppufile);
  499. ppufile.getderef(typedefderef);
  500. value:=ppufile.getexprint;
  501. { normally, the value is already compiled, so we don't need
  502. to do once again a range check
  503. }
  504. rangecheck := false;
  505. end;
  506. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  507. begin
  508. inherited ppuwrite(ppufile);
  509. ppufile.putderef(typedefderef);
  510. ppufile.putexprint(value);
  511. end;
  512. procedure tordconstnode.buildderefimpl;
  513. begin
  514. inherited buildderefimpl;
  515. typedefderef.build(typedef);
  516. end;
  517. procedure tordconstnode.derefimpl;
  518. begin
  519. inherited derefimpl;
  520. typedef:=tdef(typedefderef.resolve);
  521. end;
  522. function tordconstnode.dogetcopy : tnode;
  523. var
  524. n : tordconstnode;
  525. begin
  526. n:=tordconstnode(inherited dogetcopy);
  527. n.value:=value;
  528. n.typedef := typedef;
  529. dogetcopy:=n;
  530. end;
  531. function tordconstnode.pass_typecheck:tnode;
  532. begin
  533. result:=nil;
  534. resultdef:=typedef;
  535. { only do range checking when explicitly asked for it
  536. and if the type can be range checked, see tests/tbs/tb0539.pp }
  537. if rangecheck and (resultdef.typ in [orddef,enumdef]) then
  538. testrange(resultdef,value,false);
  539. end;
  540. function tordconstnode.pass_1 : tnode;
  541. begin
  542. result:=nil;
  543. expectloc:=LOC_CONSTANT;
  544. end;
  545. function tordconstnode.docompare(p: tnode): boolean;
  546. begin
  547. docompare :=
  548. inherited docompare(p) and
  549. (value = tordconstnode(p).value);
  550. end;
  551. procedure Tordconstnode.printnodedata(var t:text);
  552. begin
  553. inherited printnodedata(t);
  554. writeln(t,printnodeindention,'value = ',tostr(value));
  555. end;
  556. {*****************************************************************************
  557. TPOINTERCONSTNODE
  558. *****************************************************************************}
  559. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  560. begin
  561. inherited create(pointerconstn);
  562. value:=v;
  563. typedef:=def;
  564. end;
  565. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  566. begin
  567. inherited ppuload(t,ppufile);
  568. ppufile.getderef(typedefderef);
  569. value:=ppufile.getptruint;
  570. end;
  571. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  572. begin
  573. inherited ppuwrite(ppufile);
  574. ppufile.putderef(typedefderef);
  575. ppufile.putptruint(value);
  576. end;
  577. procedure tpointerconstnode.buildderefimpl;
  578. begin
  579. inherited buildderefimpl;
  580. typedefderef.build(typedef);
  581. end;
  582. procedure tpointerconstnode.derefimpl;
  583. begin
  584. inherited derefimpl;
  585. typedef:=tdef(typedefderef.resolve);
  586. end;
  587. function tpointerconstnode.dogetcopy : tnode;
  588. var
  589. n : tpointerconstnode;
  590. begin
  591. n:=tpointerconstnode(inherited dogetcopy);
  592. n.value:=value;
  593. n.typedef := typedef;
  594. dogetcopy:=n;
  595. end;
  596. function tpointerconstnode.pass_typecheck:tnode;
  597. begin
  598. result:=nil;
  599. resultdef:=typedef;
  600. end;
  601. function tpointerconstnode.pass_1 : tnode;
  602. begin
  603. result:=nil;
  604. expectloc:=LOC_CONSTANT;
  605. end;
  606. function tpointerconstnode.docompare(p: tnode): boolean;
  607. begin
  608. docompare :=
  609. inherited docompare(p) and
  610. (value = tpointerconstnode(p).value);
  611. end;
  612. {*****************************************************************************
  613. TSTRINGCONSTNODE
  614. *****************************************************************************}
  615. constructor tstringconstnode.createstr(const s : string);
  616. var
  617. l : longint;
  618. begin
  619. inherited create(stringconstn);
  620. l:=length(s);
  621. len:=l;
  622. { stringdup write even past a #0 }
  623. getmem(value_str,l+1);
  624. move(s[1],value_str^,l);
  625. value_str[l]:=#0;
  626. lab_str:=nil;
  627. cst_type:=cst_conststring;
  628. end;
  629. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  630. begin
  631. inherited create(stringconstn);
  632. len:=getlengthwidestring(w);
  633. initwidestring(pcompilerwidestring(value_str));
  634. copywidestring(w,pcompilerwidestring(value_str));
  635. lab_str:=nil;
  636. cst_type:=cst_widestring;
  637. end;
  638. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  639. begin
  640. inherited create(stringconstn);
  641. len:=l;
  642. value_str:=s;
  643. cst_type:=cst_conststring;
  644. lab_str:=nil;
  645. end;
  646. destructor tstringconstnode.destroy;
  647. begin
  648. if cst_type in [cst_widestring,cst_unicodestring] then
  649. donewidestring(pcompilerwidestring(value_str))
  650. else
  651. ansistringdispose(value_str,len);
  652. inherited destroy;
  653. end;
  654. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  655. var
  656. pw : pcompilerwidestring;
  657. begin
  658. inherited ppuload(t,ppufile);
  659. cst_type:=tconststringtype(ppufile.getbyte);
  660. len:=ppufile.getlongint;
  661. if cst_type in [cst_widestring,cst_unicodestring] then
  662. begin
  663. initwidestring(pw);
  664. setlengthwidestring(pw,len);
  665. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  666. pcompilerwidestring(value_str):=pw
  667. end
  668. else
  669. begin
  670. getmem(value_str,len+1);
  671. ppufile.getdata(value_str^,len);
  672. value_str[len]:=#0;
  673. end;
  674. lab_str:=tasmlabel(ppufile.getasmsymbol);
  675. end;
  676. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  677. begin
  678. inherited ppuwrite(ppufile);
  679. ppufile.putbyte(byte(cst_type));
  680. ppufile.putlongint(len);
  681. if cst_type in [cst_widestring,cst_unicodestring] then
  682. ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
  683. else
  684. ppufile.putdata(value_str^,len);
  685. ppufile.putasmsymbol(lab_str);
  686. end;
  687. procedure tstringconstnode.buildderefimpl;
  688. begin
  689. inherited buildderefimpl;
  690. end;
  691. procedure tstringconstnode.derefimpl;
  692. begin
  693. inherited derefimpl;
  694. end;
  695. function tstringconstnode.dogetcopy : tnode;
  696. var
  697. n : tstringconstnode;
  698. begin
  699. n:=tstringconstnode(inherited dogetcopy);
  700. n.cst_type:=cst_type;
  701. n.len:=len;
  702. n.lab_str:=lab_str;
  703. if cst_type in [cst_widestring,cst_unicodestring] then
  704. begin
  705. initwidestring(pcompilerwidestring(n.value_str));
  706. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  707. end
  708. else
  709. n.value_str:=getpcharcopy;
  710. dogetcopy:=n;
  711. end;
  712. function tstringconstnode.pass_typecheck:tnode;
  713. var
  714. l : aint;
  715. begin
  716. result:=nil;
  717. case cst_type of
  718. cst_conststring :
  719. begin
  720. { handle and store as array[0..len-1] of char }
  721. if len>0 then
  722. l:=len-1
  723. else
  724. l:=0;
  725. resultdef:=tarraydef.create(0,l,s32inttype);
  726. tarraydef(resultdef).elementdef:=cchartype;
  727. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  728. end;
  729. cst_shortstring :
  730. resultdef:=cshortstringtype;
  731. cst_ansistring :
  732. resultdef:=cansistringtype;
  733. cst_unicodestring,
  734. cst_widestring :
  735. resultdef:=cwidestringtype;
  736. cst_longstring :
  737. resultdef:=clongstringtype;
  738. end;
  739. end;
  740. function tstringconstnode.pass_1 : tnode;
  741. begin
  742. result:=nil;
  743. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and
  744. (len=0) then
  745. expectloc:=LOC_CONSTANT
  746. else
  747. expectloc:=LOC_CREFERENCE;
  748. end;
  749. function tstringconstnode.getpcharcopy : pchar;
  750. var
  751. pc : pchar;
  752. begin
  753. pc:=nil;
  754. getmem(pc,len+1);
  755. if pc=nil then
  756. Message(general_f_no_memory_left);
  757. move(value_str^,pc^,len+1);
  758. getpcharcopy:=pc;
  759. end;
  760. function tstringconstnode.docompare(p: tnode): boolean;
  761. begin
  762. docompare :=
  763. inherited docompare(p) and
  764. (len = tstringconstnode(p).len) and
  765. { Don't compare the pchars, since they may contain null chars }
  766. { Since all equal constant strings are replaced by the same }
  767. { label, the following compare should be enough (JM) }
  768. (lab_str = tstringconstnode(p).lab_str);
  769. end;
  770. procedure tstringconstnode.changestringtype(def:tdef);
  771. const
  772. st2cst : array[tstringtype] of tconststringtype = (
  773. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  774. var
  775. pw : pcompilerwidestring;
  776. pc : pchar;
  777. begin
  778. if def.typ<>stringdef then
  779. internalerror(200510011);
  780. { convert ascii 2 unicode }
  781. if (tstringdef(def).stringtype=st_widestring) and
  782. (cst_type<>cst_widestring) then
  783. begin
  784. initwidestring(pw);
  785. ascii2unicode(value_str,len,pw);
  786. ansistringdispose(value_str,len);
  787. pcompilerwidestring(value_str):=pw;
  788. end
  789. else
  790. { convert unicode 2 ascii }
  791. if (cst_type=cst_widestring) and
  792. (tstringdef(def).stringtype<>st_widestring) then
  793. begin
  794. pw:=pcompilerwidestring(value_str);
  795. getmem(pc,getlengthwidestring(pw)+1);
  796. unicode2ascii(pw,pc);
  797. donewidestring(pw);
  798. value_str:=pc;
  799. end;
  800. cst_type:=st2cst[tstringdef(def).stringtype];
  801. resultdef:=def;
  802. end;
  803. {*****************************************************************************
  804. TSETCONSTNODE
  805. *****************************************************************************}
  806. constructor tsetconstnode.create(s : pconstset;def:tdef);
  807. begin
  808. inherited create(setconstn,nil);
  809. typedef:=def;
  810. if assigned(s) then
  811. begin
  812. new(value_set);
  813. value_set^:=s^;
  814. end
  815. else
  816. value_set:=nil;
  817. end;
  818. destructor tsetconstnode.destroy;
  819. begin
  820. if assigned(value_set) then
  821. dispose(value_set);
  822. inherited destroy;
  823. end;
  824. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  825. begin
  826. inherited ppuload(t,ppufile);
  827. ppufile.getderef(typedefderef);
  828. new(value_set);
  829. ppufile.getnormalset(value_set^);
  830. end;
  831. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  832. begin
  833. inherited ppuwrite(ppufile);
  834. ppufile.putderef(typedefderef);
  835. ppufile.putnormalset(value_set^);
  836. end;
  837. procedure tsetconstnode.buildderefimpl;
  838. begin
  839. inherited buildderefimpl;
  840. typedefderef.build(typedef);
  841. end;
  842. procedure tsetconstnode.derefimpl;
  843. begin
  844. inherited derefimpl;
  845. typedef:=tdef(typedefderef.resolve);
  846. end;
  847. procedure tsetconstnode.adjustforsetbase;
  848. type
  849. setbytes = array[0..31] of byte;
  850. Psetbytes = ^setbytes;
  851. var
  852. i, diff: longint;
  853. begin
  854. { Internally, the compiler stores all sets with setbase 0, so we have }
  855. { to convert the set to its actual format in case setbase<>0 when }
  856. { writing it out }
  857. if (tsetdef(resultdef).setbase<>0) then
  858. begin
  859. if (tsetdef(resultdef).setbase and 7)<>0 then
  860. internalerror(2007091501);
  861. diff:=tsetdef(resultdef).setbase div 8;
  862. { This is endian-neutral in the new set format: in both cases, }
  863. { the first byte contains the first elements of the set. }
  864. { Since the compiler/base rtl cannot contain packed sets before }
  865. { they work for big endian, it's no problem that the code below }
  866. { is wrong for the old big endian set format (setbase cannot be }
  867. { <>0 with non-packed sets). }
  868. for i:=0 to tsetdef(resultdef).size-1 do
  869. begin
  870. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  871. Psetbytes(value_set)^[i+diff]:=0;
  872. end;
  873. end;
  874. end;
  875. function tsetconstnode.dogetcopy : tnode;
  876. var
  877. n : tsetconstnode;
  878. begin
  879. n:=tsetconstnode(inherited dogetcopy);
  880. if assigned(value_set) then
  881. begin
  882. new(n.value_set);
  883. n.value_set^:=value_set^
  884. end
  885. else
  886. n.value_set:=nil;
  887. n.typedef := typedef;
  888. n.lab_set:=lab_set;
  889. dogetcopy:=n;
  890. end;
  891. function tsetconstnode.pass_typecheck:tnode;
  892. begin
  893. result:=nil;
  894. resultdef:=typedef;
  895. end;
  896. function tsetconstnode.pass_1 : tnode;
  897. begin
  898. result:=nil;
  899. if tsetdef(resultdef).settype=smallset then
  900. expectloc:=LOC_CONSTANT
  901. else
  902. expectloc:=LOC_CREFERENCE;
  903. end;
  904. function tsetconstnode.docompare(p: tnode): boolean;
  905. begin
  906. docompare:=(inherited docompare(p)) and
  907. (value_set^=Tsetconstnode(p).value_set^);
  908. end;
  909. {*****************************************************************************
  910. TNILNODE
  911. *****************************************************************************}
  912. constructor tnilnode.create;
  913. begin
  914. inherited create(niln);
  915. end;
  916. function tnilnode.pass_typecheck:tnode;
  917. begin
  918. result:=nil;
  919. resultdef:=voidpointertype;
  920. end;
  921. function tnilnode.pass_1 : tnode;
  922. begin
  923. result:=nil;
  924. expectloc:=LOC_CONSTANT;
  925. end;
  926. {*****************************************************************************
  927. TGUIDCONSTNODE
  928. *****************************************************************************}
  929. constructor tguidconstnode.create(const g:tguid);
  930. begin
  931. inherited create(guidconstn);
  932. value:=g;
  933. end;
  934. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  935. begin
  936. inherited ppuload(t,ppufile);
  937. ppufile.getguid(value);
  938. end;
  939. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  940. begin
  941. inherited ppuwrite(ppufile);
  942. ppufile.putguid(value);
  943. end;
  944. function tguidconstnode.dogetcopy : tnode;
  945. var
  946. n : tguidconstnode;
  947. begin
  948. n:=tguidconstnode(inherited dogetcopy);
  949. n.value:=value;
  950. dogetcopy:=n;
  951. end;
  952. function tguidconstnode.pass_typecheck:tnode;
  953. begin
  954. result:=nil;
  955. resultdef:=rec_tguid;
  956. end;
  957. function tguidconstnode.pass_1 : tnode;
  958. begin
  959. result:=nil;
  960. expectloc:=LOC_CREFERENCE;
  961. end;
  962. function tguidconstnode.docompare(p: tnode): boolean;
  963. begin
  964. docompare :=
  965. inherited docompare(p) and
  966. (guid2string(value) = guid2string(tguidconstnode(p).value));
  967. end;
  968. begin
  969. crealconstnode:=trealconstnode;
  970. cordconstnode:=tordconstnode;
  971. cpointerconstnode:=tpointerconstnode;
  972. cstringconstnode:=tstringconstnode;
  973. csetconstnode:=tsetconstnode;
  974. cnilnode:=tnilnode;
  975. cguidconstnode:=tguidconstnode;
  976. end.