ncon.pas 39 KB

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