2
0

ncon.pas 36 KB

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