ncon.pas 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. );
  104. tstringconstnode = class(tnode)
  105. value_str : pchar;
  106. len : longint;
  107. lab_str : tasmlabel;
  108. cst_type : tconststringtype;
  109. constructor createstr(const s : string);virtual;
  110. constructor createpchar(s : pchar;l : longint);virtual;
  111. constructor createwstr(w : pcompilerwidestring);virtual;
  112. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  113. procedure ppuwrite(ppufile:tcompilerppufile);override;
  114. procedure buildderefimpl;override;
  115. procedure derefimpl;override;
  116. destructor destroy;override;
  117. function dogetcopy : tnode;override;
  118. function pass_1 : tnode;override;
  119. function pass_typecheck:tnode;override;
  120. function getpcharcopy : pchar;
  121. function docompare(p: tnode) : boolean; override;
  122. procedure changestringtype(def:tdef);
  123. end;
  124. tstringconstnodeclass = class of tstringconstnode;
  125. tsetconstnode = class(tunarynode)
  126. typedef : tdef;
  127. typedefderef : tderef;
  128. value_set : pconstset;
  129. lab_set : tasmlabel;
  130. constructor create(s : pconstset;def:tdef);virtual;
  131. destructor destroy;override;
  132. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  133. procedure ppuwrite(ppufile:tcompilerppufile);override;
  134. procedure buildderefimpl;override;
  135. procedure derefimpl;override;
  136. function dogetcopy : tnode;override;
  137. function pass_1 : tnode;override;
  138. function pass_typecheck:tnode;override;
  139. function docompare(p: tnode) : boolean; override;
  140. end;
  141. tsetconstnodeclass = class of tsetconstnode;
  142. tnilnode = class(tnode)
  143. constructor create;virtual;
  144. function pass_1 : tnode;override;
  145. function pass_typecheck:tnode;override;
  146. end;
  147. tnilnodeclass = class of tnilnode;
  148. tguidconstnode = class(tnode)
  149. value : tguid;
  150. constructor create(const g:tguid);virtual;
  151. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  152. procedure ppuwrite(ppufile:tcompilerppufile);override;
  153. function dogetcopy : tnode;override;
  154. function pass_1 : tnode;override;
  155. function pass_typecheck:tnode;override;
  156. function docompare(p: tnode) : boolean; override;
  157. end;
  158. tguidconstnodeclass = class of tguidconstnode;
  159. var
  160. cdataconstnode : tdataconstnodeclass;
  161. crealconstnode : trealconstnodeclass;
  162. cordconstnode : tordconstnodeclass;
  163. cpointerconstnode : tpointerconstnodeclass;
  164. cstringconstnode : tstringconstnodeclass;
  165. csetconstnode : tsetconstnodeclass;
  166. cguidconstnode : tguidconstnodeclass;
  167. cnilnode : tnilnodeclass;
  168. function genintconstnode(v : TConstExprInt) : tordconstnode;
  169. function genenumnode(v : tenumsym) : tordconstnode;
  170. { some helper routines }
  171. function get_ordinal_value(p : tnode) : TConstExprInt;
  172. function is_constresourcestringnode(p : tnode) : boolean;
  173. function is_emptyset(p : tnode):boolean;
  174. function genconstsymtree(p : tconstsym) : tnode;
  175. implementation
  176. uses
  177. cutils,
  178. verbose,systems,
  179. defutil,
  180. cpubase,cgbase,
  181. nld;
  182. function genintconstnode(v : TConstExprInt) : tordconstnode;
  183. var
  184. htype : tdef;
  185. begin
  186. int_to_type(v,htype);
  187. genintconstnode:=cordconstnode.create(v,htype,true);
  188. end;
  189. function genenumnode(v : tenumsym) : tordconstnode;
  190. var
  191. htype : tdef;
  192. begin
  193. htype:=v.definition;
  194. genenumnode:=cordconstnode.create(int64(v.value),htype,true);
  195. end;
  196. function get_ordinal_value(p : tnode) : TConstExprInt;
  197. begin
  198. get_ordinal_value:=0;
  199. if is_constnode(p) then
  200. begin
  201. if p.nodetype=ordconstn then
  202. get_ordinal_value:=tordconstnode(p).value
  203. else
  204. Message(type_e_ordinal_expr_expected);
  205. end
  206. else
  207. Message(type_e_constant_expr_expected);
  208. end;
  209. function is_constresourcestringnode(p : tnode) : boolean;
  210. begin
  211. is_constresourcestringnode:=(p.nodetype=loadn) and
  212. (tloadnode(p).symtableentry.typ=constsym) and
  213. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  214. end;
  215. function is_emptyset(p : tnode):boolean;
  216. begin
  217. is_emptyset:=(p.nodetype=setconstn) and
  218. (Tsetconstnode(p).value_set^=[]);
  219. end;
  220. function genconstsymtree(p : tconstsym) : tnode;
  221. var
  222. p1 : tnode;
  223. len : longint;
  224. pc : pchar;
  225. begin
  226. p1:=nil;
  227. case p.consttyp of
  228. constord :
  229. p1:=cordconstnode.create(p.value.valueord,p.constdef,true);
  230. conststring :
  231. begin
  232. len:=p.value.len;
  233. getmem(pc,len+1);
  234. move(pchar(p.value.valueptr)^,pc^,len);
  235. pc[len]:=#0;
  236. p1:=cstringconstnode.createpchar(pc,len);
  237. end;
  238. constreal :
  239. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
  240. constset :
  241. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.constdef);
  242. constpointer :
  243. p1:=cpointerconstnode.create(p.value.valueordptr,p.constdef);
  244. constnil :
  245. p1:=cnilnode.create;
  246. else
  247. internalerror(200205103);
  248. end;
  249. genconstsymtree:=p1;
  250. end;
  251. {*****************************************************************************
  252. TDATACONSTNODE
  253. *****************************************************************************}
  254. constructor tdataconstnode.create;
  255. begin
  256. inherited create(dataconstn);
  257. data:=tdynamicarray.create(128);
  258. end;
  259. constructor tdataconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  260. var
  261. len : aint;
  262. buf : array[0..255] of byte;
  263. begin
  264. inherited ppuload(t,ppufile);
  265. len:=ppufile.getaint;
  266. if len<4096 then
  267. data:=tdynamicarray.create(len)
  268. else
  269. data:=tdynamicarray.create(4096);
  270. while len>0 do
  271. begin
  272. if len>sizeof(buf) then
  273. begin
  274. ppufile.getdata(buf,sizeof(buf));
  275. data.write(buf,sizeof(buf));
  276. dec(len,sizeof(buf));
  277. end
  278. else
  279. begin
  280. ppufile.getdata(buf,len);
  281. data.write(buf,len);
  282. len:=0;
  283. end;
  284. end;
  285. end;
  286. destructor tdataconstnode.destroy;
  287. begin
  288. data.free;
  289. inherited destroy;
  290. end;
  291. procedure tdataconstnode.ppuwrite(ppufile:tcompilerppufile);
  292. var
  293. len : aint;
  294. buf : array[0..255] of byte;
  295. begin
  296. inherited ppuwrite(ppufile);
  297. len:=data.size;
  298. ppufile.putaint(len);
  299. data.seek(0);
  300. while len>0 do
  301. begin
  302. if len>sizeof(buf) then
  303. begin
  304. data.read(buf,sizeof(buf));
  305. ppufile.putdata(buf,sizeof(buf));
  306. dec(len,sizeof(buf));
  307. end
  308. else
  309. begin
  310. data.read(buf,len);
  311. ppufile.putdata(buf,len);
  312. len:=0;
  313. end;
  314. end;
  315. end;
  316. function tdataconstnode.dogetcopy : tnode;
  317. var
  318. n : tdataconstnode;
  319. len : aint;
  320. buf : array[0..255] of byte;
  321. begin
  322. n:=tdataconstnode(inherited dogetcopy);
  323. len:=data.size;
  324. if len<4096 then
  325. n.data:=tdynamicarray.create(len)
  326. else
  327. n.data:=tdynamicarray.create(4096);
  328. data.seek(0);
  329. while len>0 do
  330. begin
  331. if len>sizeof(buf) then
  332. begin
  333. data.read(buf,sizeof(buf));
  334. n.data.write(buf,sizeof(buf));
  335. dec(len,sizeof(buf));
  336. end
  337. else
  338. begin
  339. data.read(buf,len);
  340. n.data.write(buf,len);
  341. len:=0;
  342. end;
  343. end;
  344. dogetcopy := n;
  345. end;
  346. function tdataconstnode.pass_1 : tnode;
  347. begin
  348. result:=nil;
  349. expectloc:=LOC_CREFERENCE;
  350. end;
  351. function tdataconstnode.pass_typecheck:tnode;
  352. begin
  353. result:=nil;
  354. resultdef:=voidpointertype;
  355. end;
  356. function tdataconstnode.docompare(p: tnode) : boolean;
  357. var
  358. b1,b2 : byte;
  359. I : longint;
  360. begin
  361. docompare :=
  362. inherited docompare(p) and (data.size=tdataconstnode(p).data.size);
  363. if docompare then
  364. begin
  365. data.seek(0);
  366. tdataconstnode(p).data.seek(0);
  367. for i:=0 to data.size-1 do
  368. begin
  369. data.read(b1,1);
  370. tdataconstnode(p).data.read(b2,1);
  371. if b1<>b2 then
  372. begin
  373. docompare:=false;
  374. exit;
  375. end;
  376. end;
  377. end;
  378. end;
  379. procedure tdataconstnode.printnodedata(var t:text);
  380. var
  381. i : longint;
  382. b : byte;
  383. begin
  384. inherited printnodedata(t);
  385. write(t,printnodeindention,'data size = ',data.size,' data = ');
  386. data.seek(0);
  387. for i:=0 to data.size-1 do
  388. begin
  389. data.read(b,1);
  390. if i=data.size-1 then
  391. writeln(t,b)
  392. else
  393. write(t,b,',');
  394. end;
  395. end;
  396. procedure tdataconstnode.append(const d;len : aint);
  397. begin
  398. data.seek(data.size);
  399. data.write(d,len);
  400. end;
  401. procedure tdataconstnode.align(value : word);
  402. begin
  403. if value>maxalign then
  404. maxalign:=value;
  405. data.align(value);
  406. end;
  407. {*****************************************************************************
  408. TREALCONSTNODE
  409. *****************************************************************************}
  410. { generic code }
  411. { overridden by: }
  412. { i386 }
  413. constructor trealconstnode.create(v : bestreal;def:tdef);
  414. begin
  415. inherited create(realconstn);
  416. typedef:=def;
  417. value_real:=v;
  418. value_currency:=v;
  419. lab_real:=nil;
  420. end;
  421. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  422. var
  423. i : int64;
  424. begin
  425. inherited ppuload(t,ppufile);
  426. ppufile.getderef(typedefderef);
  427. value_real:=ppufile.getreal;
  428. i:=ppufile.getint64;
  429. value_currency:=PCurrency(@i)^;
  430. lab_real:=tasmlabel(ppufile.getasmsymbol);
  431. end;
  432. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  433. begin
  434. inherited ppuwrite(ppufile);
  435. ppufile.putderef(typedefderef);
  436. ppufile.putreal(value_real);
  437. ppufile.putint64(PInt64(@value_currency)^);
  438. ppufile.putasmsymbol(lab_real);
  439. end;
  440. procedure trealconstnode.buildderefimpl;
  441. begin
  442. inherited buildderefimpl;
  443. typedefderef.build(typedef);
  444. end;
  445. procedure trealconstnode.derefimpl;
  446. begin
  447. inherited derefimpl;
  448. typedef:=tdef(typedefderef.resolve);
  449. end;
  450. function trealconstnode.dogetcopy : tnode;
  451. var
  452. n : trealconstnode;
  453. begin
  454. n:=trealconstnode(inherited dogetcopy);
  455. n.value_real:=value_real;
  456. n.value_currency:=value_currency;
  457. n.lab_real:=lab_real;
  458. dogetcopy:=n;
  459. end;
  460. function trealconstnode.pass_typecheck:tnode;
  461. begin
  462. result:=nil;
  463. resultdef:=typedef;
  464. end;
  465. function trealconstnode.pass_1 : tnode;
  466. begin
  467. result:=nil;
  468. expectloc:=LOC_CREFERENCE;
  469. { needs to be loaded into an FPU register }
  470. registersfpu:=1;
  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=cst_widestring 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=cst_widestring 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=cst_widestring 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=cst_widestring 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_widestring :
  734. resultdef:=cwidestringtype;
  735. cst_longstring :
  736. resultdef:=clongstringtype;
  737. end;
  738. end;
  739. function tstringconstnode.pass_1 : tnode;
  740. begin
  741. result:=nil;
  742. if (cst_type in [cst_ansistring,cst_widestring]) and
  743. (len=0) then
  744. expectloc:=LOC_CONSTANT
  745. else
  746. expectloc:=LOC_CREFERENCE;
  747. end;
  748. function tstringconstnode.getpcharcopy : pchar;
  749. var
  750. pc : pchar;
  751. begin
  752. pc:=nil;
  753. getmem(pc,len+1);
  754. if pc=nil then
  755. Message(general_f_no_memory_left);
  756. move(value_str^,pc^,len+1);
  757. getpcharcopy:=pc;
  758. end;
  759. function tstringconstnode.docompare(p: tnode): boolean;
  760. begin
  761. docompare :=
  762. inherited docompare(p) and
  763. (len = tstringconstnode(p).len) and
  764. { Don't compare the pchars, since they may contain null chars }
  765. { Since all equal constant strings are replaced by the same }
  766. { label, the following compare should be enough (JM) }
  767. (lab_str = tstringconstnode(p).lab_str);
  768. end;
  769. procedure tstringconstnode.changestringtype(def:tdef);
  770. const
  771. st2cst : array[tstringtype] of tconststringtype = (
  772. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring
  773. );
  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. function tsetconstnode.dogetcopy : tnode;
  848. var
  849. n : tsetconstnode;
  850. begin
  851. n:=tsetconstnode(inherited dogetcopy);
  852. if assigned(value_set) then
  853. begin
  854. new(n.value_set);
  855. n.value_set^:=value_set^
  856. end
  857. else
  858. n.value_set:=nil;
  859. n.typedef := typedef;
  860. n.lab_set:=lab_set;
  861. dogetcopy:=n;
  862. end;
  863. function tsetconstnode.pass_typecheck:tnode;
  864. begin
  865. result:=nil;
  866. resultdef:=typedef;
  867. end;
  868. function tsetconstnode.pass_1 : tnode;
  869. begin
  870. result:=nil;
  871. if tsetdef(resultdef).settype=smallset then
  872. expectloc:=LOC_CONSTANT
  873. else
  874. expectloc:=LOC_CREFERENCE;
  875. end;
  876. function tsetconstnode.docompare(p: tnode): boolean;
  877. begin
  878. docompare:=(inherited docompare(p)) and
  879. (value_set^=Tsetconstnode(p).value_set^);
  880. end;
  881. {*****************************************************************************
  882. TNILNODE
  883. *****************************************************************************}
  884. constructor tnilnode.create;
  885. begin
  886. inherited create(niln);
  887. end;
  888. function tnilnode.pass_typecheck:tnode;
  889. begin
  890. result:=nil;
  891. resultdef:=voidpointertype;
  892. end;
  893. function tnilnode.pass_1 : tnode;
  894. begin
  895. result:=nil;
  896. expectloc:=LOC_CONSTANT;
  897. end;
  898. {*****************************************************************************
  899. TGUIDCONSTNODE
  900. *****************************************************************************}
  901. constructor tguidconstnode.create(const g:tguid);
  902. begin
  903. inherited create(guidconstn);
  904. value:=g;
  905. end;
  906. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  907. begin
  908. inherited ppuload(t,ppufile);
  909. ppufile.getguid(value);
  910. end;
  911. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  912. begin
  913. inherited ppuwrite(ppufile);
  914. ppufile.putguid(value);
  915. end;
  916. function tguidconstnode.dogetcopy : tnode;
  917. var
  918. n : tguidconstnode;
  919. begin
  920. n:=tguidconstnode(inherited dogetcopy);
  921. n.value:=value;
  922. dogetcopy:=n;
  923. end;
  924. function tguidconstnode.pass_typecheck:tnode;
  925. begin
  926. result:=nil;
  927. resultdef:=rec_tguid;
  928. end;
  929. function tguidconstnode.pass_1 : tnode;
  930. begin
  931. result:=nil;
  932. expectloc:=LOC_CREFERENCE;
  933. end;
  934. function tguidconstnode.docompare(p: tnode): boolean;
  935. begin
  936. docompare :=
  937. inherited docompare(p) and
  938. (guid2string(value) = guid2string(tguidconstnode(p).value));
  939. end;
  940. begin
  941. crealconstnode:=trealconstnode;
  942. cordconstnode:=tordconstnode;
  943. cpointerconstnode:=tpointerconstnode;
  944. cstringconstnode:=tstringconstnode;
  945. csetconstnode:=tsetconstnode;
  946. cnilnode:=tnilnode;
  947. cguidconstnode:=tguidconstnode;
  948. end.