ncon.pas 33 KB

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