ncon.pas 36 KB

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