ncon.pas 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. cst_widestring :
  737. resultdef:=cwidestringtype;
  738. cst_longstring :
  739. resultdef:=clongstringtype;
  740. end;
  741. end;
  742. function tstringconstnode.pass_1 : tnode;
  743. begin
  744. result:=nil;
  745. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and
  746. (len=0) then
  747. expectloc:=LOC_CONSTANT
  748. else
  749. expectloc:=LOC_CREFERENCE;
  750. end;
  751. function tstringconstnode.getpcharcopy : pchar;
  752. var
  753. pc : pchar;
  754. begin
  755. pc:=nil;
  756. getmem(pc,len+1);
  757. if pc=nil then
  758. Message(general_f_no_memory_left);
  759. move(value_str^,pc^,len+1);
  760. getpcharcopy:=pc;
  761. end;
  762. function tstringconstnode.docompare(p: tnode): boolean;
  763. begin
  764. docompare :=
  765. inherited docompare(p) and
  766. (len = tstringconstnode(p).len) and
  767. { Don't compare the pchars, since they may contain null chars }
  768. { Since all equal constant strings are replaced by the same }
  769. { label, the following compare should be enough (JM) }
  770. (lab_str = tstringconstnode(p).lab_str);
  771. end;
  772. procedure tstringconstnode.changestringtype(def:tdef);
  773. const
  774. st2cst : array[tstringtype] of tconststringtype = (
  775. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  776. var
  777. pw : pcompilerwidestring;
  778. pc : pchar;
  779. begin
  780. if def.typ<>stringdef then
  781. internalerror(200510011);
  782. { convert ascii 2 unicode }
  783. if (tstringdef(def).stringtype=st_widestring) and
  784. (cst_type<>cst_widestring) then
  785. begin
  786. initwidestring(pw);
  787. ascii2unicode(value_str,len,pw);
  788. ansistringdispose(value_str,len);
  789. pcompilerwidestring(value_str):=pw;
  790. end
  791. else
  792. { convert unicode 2 ascii }
  793. if (cst_type=cst_widestring) and
  794. (tstringdef(def).stringtype<>st_widestring) then
  795. begin
  796. pw:=pcompilerwidestring(value_str);
  797. getmem(pc,getlengthwidestring(pw)+1);
  798. unicode2ascii(pw,pc);
  799. donewidestring(pw);
  800. value_str:=pc;
  801. end;
  802. cst_type:=st2cst[tstringdef(def).stringtype];
  803. resultdef:=def;
  804. end;
  805. {*****************************************************************************
  806. TSETCONSTNODE
  807. *****************************************************************************}
  808. constructor tsetconstnode.create(s : pconstset;def:tdef);
  809. begin
  810. inherited create(setconstn,nil);
  811. typedef:=def;
  812. if assigned(s) then
  813. begin
  814. new(value_set);
  815. value_set^:=s^;
  816. end
  817. else
  818. value_set:=nil;
  819. end;
  820. destructor tsetconstnode.destroy;
  821. begin
  822. if assigned(value_set) then
  823. dispose(value_set);
  824. inherited destroy;
  825. end;
  826. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  827. begin
  828. inherited ppuload(t,ppufile);
  829. ppufile.getderef(typedefderef);
  830. new(value_set);
  831. ppufile.getnormalset(value_set^);
  832. end;
  833. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  834. begin
  835. inherited ppuwrite(ppufile);
  836. ppufile.putderef(typedefderef);
  837. ppufile.putnormalset(value_set^);
  838. end;
  839. procedure tsetconstnode.buildderefimpl;
  840. begin
  841. inherited buildderefimpl;
  842. typedefderef.build(typedef);
  843. end;
  844. procedure tsetconstnode.derefimpl;
  845. begin
  846. inherited derefimpl;
  847. typedef:=tdef(typedefderef.resolve);
  848. end;
  849. procedure tsetconstnode.adjustforsetbase;
  850. type
  851. setbytes = array[0..31] of byte;
  852. Psetbytes = ^setbytes;
  853. var
  854. i, diff: longint;
  855. begin
  856. { Internally, the compiler stores all sets with setbase 0, so we have }
  857. { to convert the set to its actual format in case setbase<>0 when }
  858. { writing it out }
  859. if (tsetdef(resultdef).setbase<>0) then
  860. begin
  861. if (tsetdef(resultdef).setbase and 7)<>0 then
  862. internalerror(2007091501);
  863. diff:=tsetdef(resultdef).setbase div 8;
  864. { This is endian-neutral in the new set format: in both cases, }
  865. { the first byte contains the first elements of the set. }
  866. { Since the compiler/base rtl cannot contain packed sets before }
  867. { they work for big endian, it's no problem that the code below }
  868. { is wrong for the old big endian set format (setbase cannot be }
  869. { <>0 with non-packed sets). }
  870. for i:=0 to tsetdef(resultdef).size-1 do
  871. begin
  872. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  873. Psetbytes(value_set)^[i+diff]:=0;
  874. end;
  875. end;
  876. end;
  877. function tsetconstnode.dogetcopy : tnode;
  878. var
  879. n : tsetconstnode;
  880. begin
  881. n:=tsetconstnode(inherited dogetcopy);
  882. if assigned(value_set) then
  883. begin
  884. new(n.value_set);
  885. n.value_set^:=value_set^
  886. end
  887. else
  888. n.value_set:=nil;
  889. n.typedef := typedef;
  890. n.lab_set:=lab_set;
  891. dogetcopy:=n;
  892. end;
  893. function tsetconstnode.pass_typecheck:tnode;
  894. begin
  895. result:=nil;
  896. resultdef:=typedef;
  897. end;
  898. function tsetconstnode.pass_1 : tnode;
  899. begin
  900. result:=nil;
  901. if is_smallset(resultdef) then
  902. expectloc:=LOC_CONSTANT
  903. else
  904. expectloc:=LOC_CREFERENCE;
  905. end;
  906. function tsetconstnode.docompare(p: tnode): boolean;
  907. begin
  908. docompare:=(inherited docompare(p)) and
  909. (value_set^=Tsetconstnode(p).value_set^);
  910. end;
  911. {*****************************************************************************
  912. TNILNODE
  913. *****************************************************************************}
  914. constructor tnilnode.create;
  915. begin
  916. inherited create(niln);
  917. end;
  918. function tnilnode.pass_typecheck:tnode;
  919. begin
  920. result:=nil;
  921. resultdef:=voidpointertype;
  922. end;
  923. function tnilnode.pass_1 : tnode;
  924. begin
  925. result:=nil;
  926. expectloc:=LOC_CONSTANT;
  927. end;
  928. {*****************************************************************************
  929. TGUIDCONSTNODE
  930. *****************************************************************************}
  931. constructor tguidconstnode.create(const g:tguid);
  932. begin
  933. inherited create(guidconstn);
  934. value:=g;
  935. end;
  936. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  937. begin
  938. inherited ppuload(t,ppufile);
  939. ppufile.getguid(value);
  940. end;
  941. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  942. begin
  943. inherited ppuwrite(ppufile);
  944. ppufile.putguid(value);
  945. end;
  946. function tguidconstnode.dogetcopy : tnode;
  947. var
  948. n : tguidconstnode;
  949. begin
  950. n:=tguidconstnode(inherited dogetcopy);
  951. n.value:=value;
  952. dogetcopy:=n;
  953. end;
  954. function tguidconstnode.pass_typecheck:tnode;
  955. begin
  956. result:=nil;
  957. resultdef:=rec_tguid;
  958. end;
  959. function tguidconstnode.pass_1 : tnode;
  960. begin
  961. result:=nil;
  962. expectloc:=LOC_CREFERENCE;
  963. end;
  964. function tguidconstnode.docompare(p: tnode): boolean;
  965. begin
  966. docompare :=
  967. inherited docompare(p) and
  968. (guid2string(value) = guid2string(tguidconstnode(p).value));
  969. end;
  970. begin
  971. crealconstnode:=trealconstnode;
  972. cordconstnode:=tordconstnode;
  973. cpointerconstnode:=tpointerconstnode;
  974. cstringconstnode:=tstringconstnode;
  975. csetconstnode:=tsetconstnode;
  976. cnilnode:=tnilnode;
  977. cguidconstnode:=tguidconstnode;
  978. end.