ncon.pas 39 KB

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