ncon.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. if current_settings.fputype=fpu_none then
  418. internalerror(2008022401);
  419. inherited create(realconstn);
  420. typedef:=def;
  421. value_real:=v;
  422. value_currency:=v;
  423. lab_real:=nil;
  424. end;
  425. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  426. var
  427. i : int64;
  428. begin
  429. inherited ppuload(t,ppufile);
  430. ppufile.getderef(typedefderef);
  431. value_real:=ppufile.getreal;
  432. i:=ppufile.getint64;
  433. value_currency:=PCurrency(@i)^;
  434. lab_real:=tasmlabel(ppufile.getasmsymbol);
  435. end;
  436. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  437. begin
  438. inherited ppuwrite(ppufile);
  439. ppufile.putderef(typedefderef);
  440. ppufile.putreal(value_real);
  441. ppufile.putint64(PInt64(@value_currency)^);
  442. ppufile.putasmsymbol(lab_real);
  443. end;
  444. procedure trealconstnode.buildderefimpl;
  445. begin
  446. inherited buildderefimpl;
  447. typedefderef.build(typedef);
  448. end;
  449. procedure trealconstnode.derefimpl;
  450. begin
  451. inherited derefimpl;
  452. typedef:=tdef(typedefderef.resolve);
  453. end;
  454. function trealconstnode.dogetcopy : tnode;
  455. var
  456. n : trealconstnode;
  457. begin
  458. n:=trealconstnode(inherited dogetcopy);
  459. n.value_real:=value_real;
  460. n.value_currency:=value_currency;
  461. n.lab_real:=lab_real;
  462. dogetcopy:=n;
  463. end;
  464. function trealconstnode.pass_typecheck:tnode;
  465. begin
  466. result:=nil;
  467. resultdef:=typedef;
  468. end;
  469. function trealconstnode.pass_1 : tnode;
  470. begin
  471. result:=nil;
  472. expectloc:=LOC_CREFERENCE;
  473. end;
  474. function trealconstnode.docompare(p: tnode): boolean;
  475. begin
  476. docompare :=
  477. inherited docompare(p) and
  478. (value_real = trealconstnode(p).value_real) and
  479. { floating point compares for non-numbers give strange results usually }
  480. is_number_float(value_real) and
  481. is_number_float(trealconstnode(p).value_real);
  482. end;
  483. procedure Trealconstnode.printnodedata(var t:text);
  484. begin
  485. inherited printnodedata(t);
  486. writeln(t,printnodeindention,'value = ',value_real);
  487. end;
  488. {*****************************************************************************
  489. TORDCONSTNODE
  490. *****************************************************************************}
  491. constructor tordconstnode.create(v : tconstexprint;def:tdef;_rangecheck : boolean);
  492. begin
  493. inherited create(ordconstn);
  494. value:=v;
  495. typedef:=def;
  496. rangecheck := _rangecheck;
  497. end;
  498. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  499. begin
  500. inherited ppuload(t,ppufile);
  501. ppufile.getderef(typedefderef);
  502. value:=ppufile.getexprint;
  503. { normally, the value is already compiled, so we don't need
  504. to do once again a range check
  505. }
  506. rangecheck := false;
  507. end;
  508. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  509. begin
  510. inherited ppuwrite(ppufile);
  511. ppufile.putderef(typedefderef);
  512. ppufile.putexprint(value);
  513. end;
  514. procedure tordconstnode.buildderefimpl;
  515. begin
  516. inherited buildderefimpl;
  517. typedefderef.build(typedef);
  518. end;
  519. procedure tordconstnode.derefimpl;
  520. begin
  521. inherited derefimpl;
  522. typedef:=tdef(typedefderef.resolve);
  523. end;
  524. function tordconstnode.dogetcopy : tnode;
  525. var
  526. n : tordconstnode;
  527. begin
  528. n:=tordconstnode(inherited dogetcopy);
  529. n.value:=value;
  530. n.typedef := typedef;
  531. dogetcopy:=n;
  532. end;
  533. function tordconstnode.pass_typecheck:tnode;
  534. begin
  535. result:=nil;
  536. resultdef:=typedef;
  537. { only do range checking when explicitly asked for it
  538. and if the type can be range checked, see tests/tbs/tb0539.pp }
  539. if (resultdef.typ in [orddef,enumdef]) then
  540. testrange(resultdef,value,not rangecheck)
  541. end;
  542. function tordconstnode.pass_1 : tnode;
  543. begin
  544. result:=nil;
  545. expectloc:=LOC_CONSTANT;
  546. end;
  547. function tordconstnode.docompare(p: tnode): boolean;
  548. begin
  549. docompare :=
  550. inherited docompare(p) and
  551. (value = tordconstnode(p).value);
  552. end;
  553. procedure Tordconstnode.printnodedata(var t:text);
  554. begin
  555. inherited printnodedata(t);
  556. writeln(t,printnodeindention,'value = ',tostr(value));
  557. end;
  558. {*****************************************************************************
  559. TPOINTERCONSTNODE
  560. *****************************************************************************}
  561. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  562. begin
  563. inherited create(pointerconstn);
  564. value:=v;
  565. typedef:=def;
  566. end;
  567. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  568. begin
  569. inherited ppuload(t,ppufile);
  570. ppufile.getderef(typedefderef);
  571. value:=ppufile.getptruint;
  572. end;
  573. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  574. begin
  575. inherited ppuwrite(ppufile);
  576. ppufile.putderef(typedefderef);
  577. ppufile.putptruint(value);
  578. end;
  579. procedure tpointerconstnode.buildderefimpl;
  580. begin
  581. inherited buildderefimpl;
  582. typedefderef.build(typedef);
  583. end;
  584. procedure tpointerconstnode.derefimpl;
  585. begin
  586. inherited derefimpl;
  587. typedef:=tdef(typedefderef.resolve);
  588. end;
  589. function tpointerconstnode.dogetcopy : tnode;
  590. var
  591. n : tpointerconstnode;
  592. begin
  593. n:=tpointerconstnode(inherited dogetcopy);
  594. n.value:=value;
  595. n.typedef := typedef;
  596. dogetcopy:=n;
  597. end;
  598. function tpointerconstnode.pass_typecheck:tnode;
  599. begin
  600. result:=nil;
  601. resultdef:=typedef;
  602. end;
  603. function tpointerconstnode.pass_1 : tnode;
  604. begin
  605. result:=nil;
  606. expectloc:=LOC_CONSTANT;
  607. end;
  608. function tpointerconstnode.docompare(p: tnode): boolean;
  609. begin
  610. docompare :=
  611. inherited docompare(p) and
  612. (value = tpointerconstnode(p).value);
  613. end;
  614. {*****************************************************************************
  615. TSTRINGCONSTNODE
  616. *****************************************************************************}
  617. constructor tstringconstnode.createstr(const s : string);
  618. var
  619. l : longint;
  620. begin
  621. inherited create(stringconstn);
  622. l:=length(s);
  623. len:=l;
  624. { stringdup write even past a #0 }
  625. getmem(value_str,l+1);
  626. move(s[1],value_str^,l);
  627. value_str[l]:=#0;
  628. lab_str:=nil;
  629. cst_type:=cst_conststring;
  630. end;
  631. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  632. begin
  633. inherited create(stringconstn);
  634. len:=getlengthwidestring(w);
  635. initwidestring(pcompilerwidestring(value_str));
  636. copywidestring(w,pcompilerwidestring(value_str));
  637. lab_str:=nil;
  638. cst_type:=cst_widestring;
  639. end;
  640. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  641. begin
  642. inherited create(stringconstn);
  643. len:=l;
  644. value_str:=s;
  645. cst_type:=cst_conststring;
  646. lab_str:=nil;
  647. end;
  648. destructor tstringconstnode.destroy;
  649. begin
  650. if cst_type in [cst_widestring,cst_unicodestring] then
  651. donewidestring(pcompilerwidestring(value_str))
  652. else
  653. ansistringdispose(value_str,len);
  654. inherited destroy;
  655. end;
  656. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  657. var
  658. pw : pcompilerwidestring;
  659. begin
  660. inherited ppuload(t,ppufile);
  661. cst_type:=tconststringtype(ppufile.getbyte);
  662. len:=ppufile.getlongint;
  663. if cst_type in [cst_widestring,cst_unicodestring] then
  664. begin
  665. initwidestring(pw);
  666. setlengthwidestring(pw,len);
  667. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  668. pcompilerwidestring(value_str):=pw
  669. end
  670. else
  671. begin
  672. getmem(value_str,len+1);
  673. ppufile.getdata(value_str^,len);
  674. value_str[len]:=#0;
  675. end;
  676. lab_str:=tasmlabel(ppufile.getasmsymbol);
  677. end;
  678. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  679. begin
  680. inherited ppuwrite(ppufile);
  681. ppufile.putbyte(byte(cst_type));
  682. ppufile.putlongint(len);
  683. if cst_type in [cst_widestring,cst_unicodestring] then
  684. ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
  685. else
  686. ppufile.putdata(value_str^,len);
  687. ppufile.putasmsymbol(lab_str);
  688. end;
  689. procedure tstringconstnode.buildderefimpl;
  690. begin
  691. inherited buildderefimpl;
  692. end;
  693. procedure tstringconstnode.derefimpl;
  694. begin
  695. inherited derefimpl;
  696. end;
  697. function tstringconstnode.dogetcopy : tnode;
  698. var
  699. n : tstringconstnode;
  700. begin
  701. n:=tstringconstnode(inherited dogetcopy);
  702. n.cst_type:=cst_type;
  703. n.len:=len;
  704. n.lab_str:=lab_str;
  705. if cst_type in [cst_widestring,cst_unicodestring] then
  706. begin
  707. initwidestring(pcompilerwidestring(n.value_str));
  708. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  709. end
  710. else
  711. n.value_str:=getpcharcopy;
  712. dogetcopy:=n;
  713. end;
  714. function tstringconstnode.pass_typecheck:tnode;
  715. var
  716. l : aint;
  717. begin
  718. result:=nil;
  719. case cst_type of
  720. cst_conststring :
  721. begin
  722. { handle and store as array[0..len-1] of char }
  723. if len>0 then
  724. l:=len-1
  725. else
  726. l:=0;
  727. resultdef:=tarraydef.create(0,l,s32inttype);
  728. tarraydef(resultdef).elementdef:=cchartype;
  729. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  730. end;
  731. cst_shortstring :
  732. resultdef:=cshortstringtype;
  733. cst_ansistring :
  734. resultdef:=cansistringtype;
  735. cst_unicodestring :
  736. resultdef:=cunicodestringtype;
  737. cst_widestring :
  738. resultdef:=cwidestringtype;
  739. cst_longstring :
  740. resultdef:=clongstringtype;
  741. end;
  742. end;
  743. function tstringconstnode.pass_1 : tnode;
  744. begin
  745. result:=nil;
  746. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and
  747. (len=0) then
  748. expectloc:=LOC_CONSTANT
  749. else
  750. expectloc:=LOC_CREFERENCE;
  751. end;
  752. function tstringconstnode.getpcharcopy : pchar;
  753. var
  754. pc : pchar;
  755. begin
  756. pc:=nil;
  757. getmem(pc,len+1);
  758. if pc=nil then
  759. Message(general_f_no_memory_left);
  760. move(value_str^,pc^,len+1);
  761. getpcharcopy:=pc;
  762. end;
  763. function tstringconstnode.docompare(p: tnode): boolean;
  764. begin
  765. docompare :=
  766. inherited docompare(p) and
  767. (len = tstringconstnode(p).len) and
  768. { Don't compare the pchars, since they may contain null chars }
  769. { Since all equal constant strings are replaced by the same }
  770. { label, the following compare should be enough (JM) }
  771. (lab_str = tstringconstnode(p).lab_str);
  772. end;
  773. procedure tstringconstnode.changestringtype(def:tdef);
  774. const
  775. st2cst : array[tstringtype] of tconststringtype = (
  776. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  777. var
  778. pw : pcompilerwidestring;
  779. pc : pchar;
  780. begin
  781. if def.typ<>stringdef then
  782. internalerror(200510011);
  783. { convert ascii 2 unicode }
  784. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  785. not(cst_type in [cst_widestring,cst_unicodestring]) then
  786. begin
  787. initwidestring(pw);
  788. ascii2unicode(value_str,len,pw);
  789. ansistringdispose(value_str,len);
  790. pcompilerwidestring(value_str):=pw;
  791. end
  792. else
  793. { convert unicode 2 ascii }
  794. if (cst_type in [cst_widestring,cst_unicodestring]) and
  795. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  796. begin
  797. pw:=pcompilerwidestring(value_str);
  798. getmem(pc,getlengthwidestring(pw)+1);
  799. unicode2ascii(pw,pc);
  800. donewidestring(pw);
  801. value_str:=pc;
  802. end;
  803. cst_type:=st2cst[tstringdef(def).stringtype];
  804. resultdef:=def;
  805. end;
  806. {*****************************************************************************
  807. TSETCONSTNODE
  808. *****************************************************************************}
  809. constructor tsetconstnode.create(s : pconstset;def:tdef);
  810. begin
  811. inherited create(setconstn,nil);
  812. typedef:=def;
  813. if assigned(s) then
  814. begin
  815. new(value_set);
  816. value_set^:=s^;
  817. end
  818. else
  819. value_set:=nil;
  820. end;
  821. destructor tsetconstnode.destroy;
  822. begin
  823. if assigned(value_set) then
  824. dispose(value_set);
  825. inherited destroy;
  826. end;
  827. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  828. begin
  829. inherited ppuload(t,ppufile);
  830. ppufile.getderef(typedefderef);
  831. new(value_set);
  832. ppufile.getnormalset(value_set^);
  833. end;
  834. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  835. begin
  836. inherited ppuwrite(ppufile);
  837. ppufile.putderef(typedefderef);
  838. ppufile.putnormalset(value_set^);
  839. end;
  840. procedure tsetconstnode.buildderefimpl;
  841. begin
  842. inherited buildderefimpl;
  843. typedefderef.build(typedef);
  844. end;
  845. procedure tsetconstnode.derefimpl;
  846. begin
  847. inherited derefimpl;
  848. typedef:=tdef(typedefderef.resolve);
  849. end;
  850. procedure tsetconstnode.adjustforsetbase;
  851. type
  852. setbytes = array[0..31] of byte;
  853. Psetbytes = ^setbytes;
  854. var
  855. i, diff: longint;
  856. begin
  857. { Internally, the compiler stores all sets with setbase 0, so we have }
  858. { to convert the set to its actual format in case setbase<>0 when }
  859. { writing it out }
  860. if (tsetdef(resultdef).setbase<>0) then
  861. begin
  862. if (tsetdef(resultdef).setbase and 7)<>0 then
  863. internalerror(2007091501);
  864. diff:=tsetdef(resultdef).setbase div 8;
  865. { This is endian-neutral in the new set format: in both cases, }
  866. { the first byte contains the first elements of the set. }
  867. { Since the compiler/base rtl cannot contain packed sets before }
  868. { they work for big endian, it's no problem that the code below }
  869. { is wrong for the old big endian set format (setbase cannot be }
  870. { <>0 with non-packed sets). }
  871. for i:=0 to tsetdef(resultdef).size-1 do
  872. begin
  873. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  874. Psetbytes(value_set)^[i+diff]:=0;
  875. end;
  876. end;
  877. end;
  878. function tsetconstnode.dogetcopy : tnode;
  879. var
  880. n : tsetconstnode;
  881. begin
  882. n:=tsetconstnode(inherited dogetcopy);
  883. if assigned(value_set) then
  884. begin
  885. new(n.value_set);
  886. n.value_set^:=value_set^
  887. end
  888. else
  889. n.value_set:=nil;
  890. n.typedef := typedef;
  891. n.lab_set:=lab_set;
  892. dogetcopy:=n;
  893. end;
  894. function tsetconstnode.pass_typecheck:tnode;
  895. begin
  896. result:=nil;
  897. resultdef:=typedef;
  898. end;
  899. function tsetconstnode.pass_1 : tnode;
  900. begin
  901. result:=nil;
  902. if is_smallset(resultdef) then
  903. expectloc:=LOC_CONSTANT
  904. else
  905. expectloc:=LOC_CREFERENCE;
  906. end;
  907. function tsetconstnode.docompare(p: tnode): boolean;
  908. begin
  909. docompare:=(inherited docompare(p)) and
  910. (value_set^=Tsetconstnode(p).value_set^);
  911. end;
  912. {*****************************************************************************
  913. TNILNODE
  914. *****************************************************************************}
  915. constructor tnilnode.create;
  916. begin
  917. inherited create(niln);
  918. end;
  919. function tnilnode.pass_typecheck:tnode;
  920. begin
  921. result:=nil;
  922. resultdef:=voidpointertype;
  923. end;
  924. function tnilnode.pass_1 : tnode;
  925. begin
  926. result:=nil;
  927. expectloc:=LOC_CONSTANT;
  928. end;
  929. {*****************************************************************************
  930. TGUIDCONSTNODE
  931. *****************************************************************************}
  932. constructor tguidconstnode.create(const g:tguid);
  933. begin
  934. inherited create(guidconstn);
  935. value:=g;
  936. end;
  937. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  938. begin
  939. inherited ppuload(t,ppufile);
  940. ppufile.getguid(value);
  941. end;
  942. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  943. begin
  944. inherited ppuwrite(ppufile);
  945. ppufile.putguid(value);
  946. end;
  947. function tguidconstnode.dogetcopy : tnode;
  948. var
  949. n : tguidconstnode;
  950. begin
  951. n:=tguidconstnode(inherited dogetcopy);
  952. n.value:=value;
  953. dogetcopy:=n;
  954. end;
  955. function tguidconstnode.pass_typecheck:tnode;
  956. begin
  957. result:=nil;
  958. resultdef:=rec_tguid;
  959. end;
  960. function tguidconstnode.pass_1 : tnode;
  961. begin
  962. result:=nil;
  963. expectloc:=LOC_CREFERENCE;
  964. end;
  965. function tguidconstnode.docompare(p: tnode): boolean;
  966. begin
  967. docompare :=
  968. inherited docompare(p) and
  969. (guid2string(value) = guid2string(tguidconstnode(p).value));
  970. end;
  971. begin
  972. crealconstnode:=trealconstnode;
  973. cordconstnode:=tordconstnode;
  974. cpointerconstnode:=tpointerconstnode;
  975. cstringconstnode:=tstringconstnode;
  976. csetconstnode:=tsetconstnode;
  977. cnilnode:=tnilnode;
  978. cguidconstnode:=tguidconstnode;
  979. end.