ncon.pas 40 KB

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