ncon.pas 40 KB

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