ncon.pas 35 KB

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