ncon.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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,aasmcnst,cpuinfo,globals,
  24. symconst,symtype,symdef,symsym;
  25. type
  26. tconstnode = class abstract(tnode)
  27. { directly emit a node's constant data as a constant and return the
  28. amount of data written }
  29. function emit_data(tcb:ttai_typedconstbuilder):sizeint;virtual;abstract;
  30. end;
  31. trealconstnode = class(tconstnode)
  32. typedef : tdef;
  33. typedefderef : tderef;
  34. value_real : bestreal;
  35. value_currency : currency;
  36. lab_real : tasmlabel;
  37. constructor create(v : bestreal;def:tdef);virtual;
  38. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  39. procedure ppuwrite(ppufile:tcompilerppufile);override;
  40. procedure buildderefimpl;override;
  41. procedure derefimpl;override;
  42. function dogetcopy : tnode;override;
  43. function pass_1 : tnode;override;
  44. function pass_typecheck:tnode;override;
  45. function docompare(p: tnode) : boolean; override;
  46. procedure printnodedata(var t:text);override;
  47. function emit_data(tcb:ttai_typedconstbuilder):sizeint; override;
  48. {$ifdef DEBUG_NODE_XML}
  49. procedure XMLPrintNodeData(var T: Text); override;
  50. {$endif DEBUG_NODE_XML}
  51. end;
  52. trealconstnodeclass = class of trealconstnode;
  53. tordconstnode = class(tconstnode)
  54. typedef : tdef;
  55. typedefderef : tderef;
  56. value : TConstExprInt;
  57. rangecheck : boolean;
  58. { create an ordinal constant node of the specified type and value.
  59. _rangecheck determines if the value of the ordinal should be checked
  60. against the ranges of the type definition.
  61. }
  62. constructor create(const v : tconstexprint;def:tdef; _rangecheck : boolean);virtual;
  63. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  64. procedure ppuwrite(ppufile:tcompilerppufile);override;
  65. procedure buildderefimpl;override;
  66. procedure derefimpl;override;
  67. function dogetcopy : tnode;override;
  68. function pass_1 : tnode;override;
  69. function pass_typecheck:tnode;override;
  70. function docompare(p: tnode) : boolean; override;
  71. procedure printnodedata(var t:text);override;
  72. function emit_data(tcb:ttai_typedconstbuilder):sizeint; override;
  73. {$ifdef DEBUG_NODE_XML}
  74. procedure XMLPrintNodeInfo(var T: Text); override;
  75. procedure XMLPrintNodeData(var T: Text); override;
  76. {$endif DEBUG_NODE_XML}
  77. end;
  78. tordconstnodeclass = class of tordconstnode;
  79. tpointerconstnode = class(tconstnode)
  80. typedef : tdef;
  81. typedefderef : tderef;
  82. value : TConstPtrUInt;
  83. constructor create(v : TConstPtrUInt;def:tdef);virtual;
  84. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  85. procedure ppuwrite(ppufile:tcompilerppufile);override;
  86. procedure buildderefimpl;override;
  87. procedure derefimpl;override;
  88. function dogetcopy : tnode;override;
  89. function pass_1 : tnode;override;
  90. function pass_typecheck:tnode;override;
  91. function docompare(p: tnode) : boolean; override;
  92. procedure printnodedata(var t : text); override;
  93. function emit_data(tcb:ttai_typedconstbuilder):sizeint; override;
  94. {$ifdef DEBUG_NODE_XML}
  95. procedure XMLPrintNodeData(var T: Text); override;
  96. {$endif DEBUG_NODE_XML}
  97. end;
  98. tpointerconstnodeclass = class of tpointerconstnode;
  99. tconststringtype = (
  100. cst_conststring,
  101. cst_shortstring,
  102. cst_longstring,
  103. cst_ansistring,
  104. cst_widestring,
  105. cst_unicodestring
  106. );
  107. tstringconstnode = class(tconstnode)
  108. value_str : pchar;
  109. len : longint;
  110. lab_str : tasmlabel;
  111. astringdef : tdef;
  112. astringdefderef : tderef;
  113. cst_type : tconststringtype;
  114. constructor createstr(const s : string);virtual;
  115. constructor createpchar(s: pchar; l: longint; def: tdef);virtual;
  116. constructor createunistr(w : pcompilerwidestring);virtual;
  117. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. procedure buildderefimpl;override;
  120. procedure derefimpl;override;
  121. destructor destroy;override;
  122. function dogetcopy : tnode;override;
  123. function pass_1 : tnode;override;
  124. function pass_typecheck:tnode;override;
  125. function getpcharcopy : pchar;
  126. function docompare(p: tnode) : boolean; override;
  127. procedure changestringtype(def:tdef);
  128. function fullcompare(p: tstringconstnode): longint;
  129. function emit_data(tcb:ttai_typedconstbuilder):sizeint; override;
  130. { returns whether this platform uses the nil pointer to represent
  131. empty dynamic strings }
  132. class function emptydynstrnil: boolean; virtual;
  133. {$ifdef DEBUG_NODE_XML}
  134. procedure XMLPrintNodeData(var T: Text); override;
  135. {$endif DEBUG_NODE_XML}
  136. end;
  137. tstringconstnodeclass = class of tstringconstnode;
  138. tsetconstnode = class(tunarynode)
  139. typedef : tdef;
  140. typedefderef : tderef;
  141. value_set : pconstset;
  142. lab_set : tasmsymbol;
  143. constructor create(s : pconstset;def:tdef);virtual;
  144. destructor destroy;override;
  145. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  146. procedure ppuwrite(ppufile:tcompilerppufile);override;
  147. procedure buildderefimpl;override;
  148. procedure derefimpl;override;
  149. procedure adjustforsetbase;
  150. function dogetcopy : tnode;override;
  151. function pass_1 : tnode;override;
  152. function pass_typecheck:tnode;override;
  153. function docompare(p: tnode) : boolean; override;
  154. function elements : AInt;
  155. function emit_data(tcb:ttai_typedconstbuilder):sizeint;
  156. end;
  157. tsetconstnodeclass = class of tsetconstnode;
  158. tnilnode = class(tconstnode)
  159. constructor create;virtual;
  160. function pass_1 : tnode;override;
  161. function pass_typecheck:tnode;override;
  162. function emit_data(tcb:ttai_typedconstbuilder):sizeint; override;
  163. end;
  164. tnilnodeclass = class of tnilnode;
  165. tguidconstnode = class(tconstnode)
  166. value : tguid;
  167. lab_set : tasmsymbol;
  168. constructor create(const g:tguid);virtual;
  169. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  170. procedure ppuwrite(ppufile:tcompilerppufile);override;
  171. function dogetcopy : tnode;override;
  172. function pass_1 : tnode;override;
  173. function pass_typecheck:tnode;override;
  174. function docompare(p: tnode) : boolean; override;
  175. function emit_data(tcb:ttai_typedconstbuilder):sizeint; override;
  176. end;
  177. tguidconstnodeclass = class of tguidconstnode;
  178. var
  179. crealconstnode : trealconstnodeclass = trealconstnode;
  180. cordconstnode : tordconstnodeclass = tordconstnode;
  181. cpointerconstnode : tpointerconstnodeclass = tpointerconstnode;
  182. cstringconstnode : tstringconstnodeclass = tstringconstnode;
  183. csetconstnode : tsetconstnodeclass = tsetconstnode;
  184. cguidconstnode : tguidconstnodeclass = tguidconstnode;
  185. cnilnode : tnilnodeclass=tnilnode;
  186. function genintconstnode(const v : TConstExprInt) : tordconstnode;
  187. function genenumnode(v : tenumsym) : tordconstnode;
  188. { some helper routines }
  189. function get_ordinal_value(p : tnode) : TConstExprInt;
  190. function get_string_value(p : tnode; def: tstringdef) : tstringconstnode;
  191. function is_constresourcestringnode(p : tnode) : boolean;
  192. function is_emptyset(p : tnode):boolean;
  193. function genconstsymtree(p : tconstsym) : tnode;
  194. function getbooleanvalue(p : tnode) : boolean;
  195. implementation
  196. uses
  197. cutils,
  198. verbose,systems,sysutils,ppu,
  199. defcmp,defutil,procinfo,
  200. aasmdata,aasmtai,
  201. cgbase,
  202. nld;
  203. function genintconstnode(const v : TConstExprInt) : tordconstnode;
  204. var
  205. htype : tdef;
  206. begin
  207. int_to_type(v,htype);
  208. genintconstnode:=cordconstnode.create(v,htype,true);
  209. end;
  210. function genenumnode(v : tenumsym) : tordconstnode;
  211. var
  212. htype : tdef;
  213. begin
  214. htype:=v.definition;
  215. genenumnode:=cordconstnode.create(int64(v.value),htype,true);
  216. end;
  217. function get_ordinal_value(p : tnode) : TConstExprInt;
  218. begin
  219. get_ordinal_value:=0;
  220. if is_constnode(p) then
  221. begin
  222. if p.nodetype=ordconstn then
  223. get_ordinal_value:=tordconstnode(p).value
  224. else
  225. Message(type_e_ordinal_expr_expected);
  226. end
  227. else
  228. Message(type_e_constant_expr_expected);
  229. end;
  230. function get_string_value(p: tnode; def: tstringdef): tstringconstnode;
  231. var
  232. stringVal: string;
  233. pWideStringVal: pcompilerwidestring;
  234. begin
  235. stringVal:='';
  236. if is_constcharnode(p) then
  237. begin
  238. SetLength(stringVal,1);
  239. stringVal[1]:=char(tordconstnode(p).value.uvalue);
  240. result:=cstringconstnode.createstr(stringVal);
  241. end
  242. else if is_constwidecharnode(p) then
  243. begin
  244. initwidestring(pWideStringVal);
  245. concatwidestringchar(pWideStringVal, tcompilerwidechar(tordconstnode(p).value.uvalue));
  246. result:=cstringconstnode.createunistr(pWideStringVal);
  247. end
  248. else if p.nodetype=stringconstn then
  249. result:=tstringconstnode(p.getcopy)
  250. else
  251. begin
  252. Message(type_e_string_expr_expected);
  253. stringVal:='';
  254. result:=cstringconstnode.createstr(stringVal);
  255. end;
  256. result.changestringtype(def);
  257. end;
  258. function is_constresourcestringnode(p : tnode) : boolean;
  259. begin
  260. is_constresourcestringnode:=(p.nodetype=loadn) and
  261. (tloadnode(p).symtableentry.typ=constsym) and
  262. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  263. end;
  264. function is_emptyset(p : tnode):boolean;
  265. begin
  266. is_emptyset:=(p.nodetype=setconstn) and
  267. (Tsetconstnode(p).value_set^=[]);
  268. end;
  269. function genconstsymtree(p : tconstsym) : tnode;
  270. var
  271. p1 : tnode;
  272. len : longint;
  273. pc : pchar;
  274. value_set : pconstset;
  275. begin
  276. p1:=nil;
  277. case p.consttyp of
  278. constord :
  279. begin
  280. if p.constdef=nil then
  281. internalerror(200403232);
  282. p1:=cordconstnode.create(p.value.valueord,p.constdef,true);
  283. end;
  284. conststring :
  285. begin
  286. len:=p.value.len;
  287. if not(cs_refcountedstrings in current_settings.localswitches) and (len>255) then
  288. begin
  289. message(parser_e_string_const_too_long);
  290. len:=255;
  291. end;
  292. getmem(pc,len+1);
  293. move(pchar(p.value.valueptr)^,pc^,len);
  294. pc[len]:=#0;
  295. p1:=cstringconstnode.createpchar(pc,len,p.constdef);
  296. end;
  297. constwstring :
  298. p1:=cstringconstnode.createunistr(pcompilerwidestring(p.value.valueptr));
  299. constreal :
  300. begin
  301. if (sp_generic_para in p.symoptions) and not (sp_generic_const in p.symoptions) then
  302. p1:=crealconstnode.create(default(bestreal),p.constdef)
  303. else
  304. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,p.constdef);
  305. end;
  306. constset :
  307. begin
  308. if sp_generic_const in p.symoptions then
  309. begin
  310. new(value_set);
  311. value_set^:=pconstset(p.value.valueptr)^;
  312. p1:=csetconstnode.create(value_set,p.constdef);
  313. end
  314. else if sp_generic_para in p.symoptions then
  315. begin
  316. new(value_set);
  317. p1:=csetconstnode.create(value_set,p.constdef);
  318. end
  319. else
  320. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.constdef);
  321. end;
  322. constpointer :
  323. begin
  324. if sp_generic_para in p.symoptions then
  325. p1:=cpointerconstnode.create(default(tconstptruint),p.constdef)
  326. else
  327. p1:=cpointerconstnode.create(p.value.valueordptr,p.constdef);
  328. end;
  329. constnil :
  330. p1:=cnilnode.create;
  331. constguid :
  332. begin
  333. if sp_generic_para in p.symoptions then
  334. p1:=cguidconstnode.create(default(tguid))
  335. else
  336. p1:=cguidconstnode.create(pguid(p.value.valueptr)^);
  337. end;
  338. else
  339. internalerror(200205103);
  340. end;
  341. { transfer generic param flag from symbol to node }
  342. if sp_generic_para in p.symoptions then
  343. include(p1.flags,nf_generic_para);
  344. genconstsymtree:=p1;
  345. end;
  346. function getbooleanvalue(p : tnode) : boolean;
  347. begin
  348. if is_constboolnode(p) then
  349. result:=tordconstnode(p).value<>0
  350. else
  351. internalerror(2013111601);
  352. end;
  353. {*****************************************************************************
  354. TREALCONSTNODE
  355. *****************************************************************************}
  356. { generic code }
  357. { overridden by: }
  358. { i386 }
  359. constructor trealconstnode.create(v : bestreal;def:tdef);
  360. begin
  361. if current_settings.fputype=fpu_none then
  362. internalerror(2008022401);
  363. inherited create(realconstn);
  364. typedef:=def;
  365. case tfloatdef(def).floattype of
  366. s32real:
  367. v:=single(v);
  368. s64real:
  369. v:=double(v);
  370. s80real,
  371. sc80real,
  372. s64comp,
  373. s64currency:
  374. v:=extended(v);
  375. s128real:
  376. internalerror(2013102701);
  377. end;
  378. value_real:=v;
  379. value_currency:=v;
  380. lab_real:=nil;
  381. end;
  382. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  383. var
  384. i : int64;
  385. begin
  386. inherited ppuload(t,ppufile);
  387. ppufile.getderef(typedefderef);
  388. value_real:=ppufile.getreal;
  389. i:=ppufile.getint64;
  390. value_currency:=PCurrency(@i)^;
  391. lab_real:=tasmlabel(ppufile.getasmsymbol);
  392. end;
  393. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  394. begin
  395. inherited ppuwrite(ppufile);
  396. ppufile.putderef(typedefderef);
  397. ppufile.putreal(value_real);
  398. ppufile.putint64(PInt64(@value_currency)^);
  399. ppufile.putasmsymbol(lab_real);
  400. end;
  401. procedure trealconstnode.buildderefimpl;
  402. begin
  403. inherited buildderefimpl;
  404. typedefderef.build(typedef);
  405. end;
  406. procedure trealconstnode.derefimpl;
  407. begin
  408. inherited derefimpl;
  409. typedef:=tdef(typedefderef.resolve);
  410. end;
  411. function trealconstnode.dogetcopy : tnode;
  412. var
  413. n : trealconstnode;
  414. begin
  415. n:=trealconstnode(inherited dogetcopy);
  416. n.typedef:=typedef;
  417. n.value_real:=value_real;
  418. n.value_currency:=value_currency;
  419. n.lab_real:=lab_real;
  420. dogetcopy:=n;
  421. end;
  422. function trealconstnode.pass_typecheck:tnode;
  423. begin
  424. result:=nil;
  425. resultdef:=typedef;
  426. { range checking? }
  427. if floating_point_range_check_error or
  428. (tfloatdef(resultdef).floattype in [s64comp,s64currency]) then
  429. begin
  430. { use CGMessage so that the resultdef will get set to errordef
  431. by pass1.typecheckpass_internal if a range error was triggered,
  432. which in turn will prevent any potential parent type conversion
  433. node from creating a new realconstnode with this exact same value
  434. and hence trigger the same error again }
  435. case tfloatdef(resultdef).floattype of
  436. s32real :
  437. begin
  438. if ts32real(value_real)=MathInf.Value then
  439. CGMessage(parser_e_range_check_error);
  440. end;
  441. s64real:
  442. begin
  443. if ts64real(value_real)=MathInf.Value then
  444. CGMessage(parser_e_range_check_error);
  445. end;
  446. s80real,
  447. sc80real:
  448. begin
  449. if ts80real(value_real)=MathInf.Value then
  450. CGMessage(parser_e_range_check_error);
  451. end;
  452. s64comp,
  453. s64currency:
  454. begin
  455. if (value_real>9223372036854775807.0) or
  456. (value_real<-9223372036854775808.0) then
  457. CGMessage(parser_e_range_check_error)
  458. end;
  459. s128real:
  460. begin
  461. if ts128real(value_real)=MathInf.Value then
  462. CGMessage(parser_e_range_check_error);
  463. end;
  464. end;
  465. end;
  466. end;
  467. function trealconstnode.pass_1 : tnode;
  468. begin
  469. result:=nil;
  470. expectloc:=LOC_CREFERENCE;
  471. end;
  472. function trealconstnode.docompare(p: tnode): boolean;
  473. begin
  474. docompare :=
  475. inherited docompare(p) and
  476. { this should be always true }
  477. (trealconstnode(p).typedef.typ=floatdef) and (typedef.typ=floatdef) and
  478. (tfloatdef(typedef).floattype = tfloatdef(trealconstnode(p).typedef).floattype) and
  479. (
  480. (
  481. (tfloatdef(typedef).floattype=s64currency) and
  482. (value_currency=trealconstnode(p).value_currency)
  483. )
  484. or
  485. (
  486. (tfloatdef(typedef).floattype<>s64currency) and
  487. (value_real = trealconstnode(p).value_real) and
  488. { floating point compares for non-numbers give strange results usually }
  489. is_number_float(value_real) and
  490. is_number_float(trealconstnode(p).value_real)
  491. )
  492. );
  493. end;
  494. procedure trealconstnode.printnodedata(var t: text);
  495. begin
  496. inherited printnodedata(t);
  497. write(t,printnodeindention,'value = ',value_real);
  498. if is_currency(resultdef) then
  499. writeln(', value_currency = ',value_currency)
  500. else
  501. writeln;
  502. end;
  503. function trealconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  504. begin
  505. case tfloatdef(typedef).floattype of
  506. s32real:
  507. tcb.emit_tai(tai_realconst.create_s32real(value_real),typedef);
  508. s64real:
  509. tcb.emit_tai(tai_realconst.create_s64real(value_real),typedef);
  510. s80real:
  511. tcb.emit_tai(tai_realconst.create_s80real(value_real,s80floattype.size),typedef);
  512. sc80real:
  513. tcb.emit_tai(tai_realconst.create_s80real(value_real,sc80floattype.size),typedef);
  514. s64comp:
  515. { the round is necessary for native compilers where comp isn't a float }
  516. tcb.emit_tai(tai_realconst.create_s64compreal(round(value_real)),typedef);
  517. s64currency:
  518. { we don't need to multiply by 10000 here }
  519. tcb.emit_tai(tai_realconst.create_s64compreal(round(value_real)),typedef);
  520. s128real:
  521. internalerror(2019070804);
  522. end;
  523. result:=resultdef.size;
  524. end;
  525. {$ifdef DEBUG_NODE_XML}
  526. procedure TRealConstNode.XMLPrintNodeData(var T: Text);
  527. begin
  528. inherited XMLPrintNodeData(T);
  529. WriteLn(T, printnodeindention, '<value>', value_real, '</value>');
  530. end;
  531. {$endif DEBUG_NODE_XML}
  532. {*****************************************************************************
  533. TORDCONSTNODE
  534. *****************************************************************************}
  535. constructor tordconstnode.create(const v : tconstexprint;def:tdef;_rangecheck : boolean);
  536. begin
  537. inherited create(ordconstn);
  538. value:=v;
  539. typedef:=def;
  540. rangecheck := _rangecheck;
  541. end;
  542. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  543. begin
  544. inherited ppuload(t,ppufile);
  545. ppufile.getderef(typedefderef);
  546. value:=ppufile.getexprint;
  547. { normally, the value is already compiled, so we don't need
  548. to do once again a range check
  549. }
  550. rangecheck := false;
  551. end;
  552. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  553. begin
  554. inherited ppuwrite(ppufile);
  555. ppufile.putderef(typedefderef);
  556. ppufile.putexprint(value);
  557. end;
  558. procedure tordconstnode.buildderefimpl;
  559. begin
  560. inherited buildderefimpl;
  561. typedefderef.build(typedef);
  562. end;
  563. procedure tordconstnode.derefimpl;
  564. begin
  565. inherited derefimpl;
  566. typedef:=tdef(typedefderef.resolve);
  567. end;
  568. function tordconstnode.dogetcopy : tnode;
  569. var
  570. n : tordconstnode;
  571. begin
  572. n:=tordconstnode(inherited dogetcopy);
  573. n.value:=value;
  574. n.typedef := typedef;
  575. dogetcopy:=n;
  576. end;
  577. function tordconstnode.pass_typecheck:tnode;
  578. begin
  579. result:=nil;
  580. resultdef:=typedef;
  581. { only do range checking when explicitly asked for it
  582. and if the type can be range checked, see tests/tbs/tb0539.pp }
  583. if (resultdef.typ in [orddef,enumdef]) and not(nf_generic_para in flags) then
  584. adaptrange(resultdef,value,nf_internal in flags,not rangecheck,rangecheck)
  585. end;
  586. function tordconstnode.pass_1 : tnode;
  587. begin
  588. result:=nil;
  589. expectloc:=LOC_CONSTANT;
  590. end;
  591. function tordconstnode.docompare(p: tnode): boolean;
  592. begin
  593. docompare :=
  594. inherited docompare(p) and
  595. (value = tordconstnode(p).value) and
  596. equal_defs(typedef,tordconstnode(p).typedef);
  597. end;
  598. procedure tordconstnode.printnodedata(var t: text);
  599. begin
  600. inherited printnodedata(t);
  601. writeln(t,printnodeindention,'typedef = "',typedef.GetTypeName,'"');
  602. writeln(t,printnodeindention,'value = ',tostr(value));
  603. end;
  604. function tordconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  605. begin
  606. tcb.emit_ord_const(value,resultdef);
  607. result:=resultdef.size;
  608. end;
  609. {$ifdef DEBUG_NODE_XML}
  610. procedure TOrdConstNode.XMLPrintNodeInfo(var T: Text);
  611. begin
  612. inherited XMLPrintNodeInfo(T);
  613. Write(T, ' rangecheck="', rangecheck, '"');
  614. end;
  615. procedure TOrdConstNode.XMLPrintNodeData(var T: Text);
  616. begin
  617. inherited XMLPrintNodeData(T);
  618. WriteLn(T, printnodeindention, '<value>', tostr(value), '</value>');
  619. end;
  620. {$endif DEBUG_NODE_XML}
  621. {*****************************************************************************
  622. TPOINTERCONSTNODE
  623. *****************************************************************************}
  624. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  625. begin
  626. inherited create(pointerconstn);
  627. value:=v;
  628. typedef:=def;
  629. end;
  630. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  631. begin
  632. inherited ppuload(t,ppufile);
  633. ppufile.getderef(typedefderef);
  634. value:=ppufile.getptruint;
  635. end;
  636. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  637. begin
  638. inherited ppuwrite(ppufile);
  639. ppufile.putderef(typedefderef);
  640. ppufile.putptruint(value);
  641. end;
  642. procedure tpointerconstnode.buildderefimpl;
  643. begin
  644. inherited buildderefimpl;
  645. typedefderef.build(typedef);
  646. end;
  647. procedure tpointerconstnode.derefimpl;
  648. begin
  649. inherited derefimpl;
  650. typedef:=tdef(typedefderef.resolve);
  651. end;
  652. function tpointerconstnode.dogetcopy : tnode;
  653. var
  654. n : tpointerconstnode;
  655. begin
  656. n:=tpointerconstnode(inherited dogetcopy);
  657. n.value:=value;
  658. n.typedef := typedef;
  659. dogetcopy:=n;
  660. end;
  661. function tpointerconstnode.pass_typecheck:tnode;
  662. begin
  663. result:=nil;
  664. resultdef:=typedef;
  665. end;
  666. function tpointerconstnode.pass_1 : tnode;
  667. begin
  668. result:=nil;
  669. expectloc:=LOC_CONSTANT;
  670. end;
  671. function tpointerconstnode.docompare(p: tnode): boolean;
  672. begin
  673. docompare :=
  674. inherited docompare(p) and
  675. (value = tpointerconstnode(p).value);
  676. end;
  677. procedure tpointerconstnode.printnodedata(var t : text);
  678. begin
  679. inherited printnodedata(t);
  680. writeln(t,printnodeindention,'value = $',hexstr(PUInt(value),sizeof(PUInt)*2));
  681. end;
  682. function tpointerconstnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  683. begin
  684. if tpointerdef(resultdef).compatible_with_pointerdef_size(tpointerdef(voidpointertype)) then
  685. tcb.emit_tai(tai_const.Create_int_dataptr(value),voidpointertype)
  686. else
  687. tcb.emit_tai(tai_const.Create_int_codeptr(value),voidcodepointertype);
  688. result:=resultdef.size;
  689. end;
  690. {$ifdef DEBUG_NODE_XML}
  691. procedure TPointerConstNode.XMLPrintNodeData(var T: Text);
  692. begin
  693. inherited XMLPrintNodeData(T);
  694. WriteLn(T, PrintNodeIndention, '<value>$', hexstr(PUInt(value),sizeof(PUInt)*2), '</value>');
  695. end;
  696. {$endif DEBUG_NODE_XML}
  697. {*****************************************************************************
  698. TSTRINGCONSTNODE
  699. *****************************************************************************}
  700. constructor tstringconstnode.createstr(const s : string);
  701. var
  702. l : longint;
  703. begin
  704. inherited create(stringconstn);
  705. l:=length(s);
  706. len:=l;
  707. { stringdup write even past a #0 }
  708. getmem(value_str,l+1);
  709. move(s[1],value_str^,l);
  710. value_str[l]:=#0;
  711. lab_str:=nil;
  712. cst_type:=cst_conststring;
  713. end;
  714. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  715. begin
  716. inherited create(stringconstn);
  717. len:=getlengthwidestring(w);
  718. initwidestring(pcompilerwidestring(value_str));
  719. copywidestring(w,pcompilerwidestring(value_str));
  720. lab_str:=nil;
  721. cst_type:=cst_unicodestring;
  722. end;
  723. constructor tstringconstnode.createpchar(s: pchar; l: longint; def: tdef);
  724. begin
  725. inherited create(stringconstn);
  726. len:=l;
  727. value_str:=s;
  728. if assigned(def) and
  729. is_ansistring(def) then
  730. begin
  731. cst_type:=cst_ansistring;
  732. astringdef:=def;
  733. end
  734. else
  735. cst_type:=cst_conststring;
  736. lab_str:=nil;
  737. end;
  738. destructor tstringconstnode.destroy;
  739. begin
  740. if cst_type in [cst_widestring,cst_unicodestring] then
  741. donewidestring(pcompilerwidestring(value_str))
  742. else
  743. ansistringdispose(value_str,len);
  744. inherited destroy;
  745. end;
  746. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  747. var
  748. pw : pcompilerwidestring;
  749. i : longint;
  750. begin
  751. inherited ppuload(t,ppufile);
  752. cst_type:=tconststringtype(ppufile.getbyte);
  753. len:=ppufile.getlongint;
  754. if cst_type in [cst_widestring,cst_unicodestring] then
  755. begin
  756. initwidestring(pw);
  757. setlengthwidestring(pw,len);
  758. { don't use getdata, because the compilerwidechars may have to
  759. be byteswapped
  760. }
  761. {$if sizeof(tcompilerwidechar) = 2}
  762. for i:=0 to pw^.len-1 do
  763. pw^.data[i]:=ppufile.getword;
  764. {$elseif sizeof(tcompilerwidechar) = 4}
  765. for i:=0 to pw^.len-1 do
  766. pw^.data[i]:=cardinal(ppufile.getlongint);
  767. {$else}
  768. {$error Unsupported tcompilerwidechar size}
  769. {$endif}
  770. pcompilerwidestring(value_str):=pw
  771. end
  772. else
  773. begin
  774. getmem(value_str,len+1);
  775. ppufile.getdata(value_str^,len);
  776. value_str[len]:=#0;
  777. end;
  778. lab_str:=tasmlabel(ppufile.getasmsymbol);
  779. if cst_type=cst_ansistring then
  780. ppufile.getderef(astringdefderef);
  781. end;
  782. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  783. begin
  784. inherited ppuwrite(ppufile);
  785. ppufile.putbyte(byte(cst_type));
  786. ppufile.putlongint(len);
  787. if cst_type in [cst_widestring,cst_unicodestring] then
  788. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  789. else
  790. ppufile.putdata(value_str^,len);
  791. ppufile.putasmsymbol(lab_str);
  792. if cst_type=cst_ansistring then
  793. ppufile.putderef(astringdefderef);
  794. end;
  795. procedure tstringconstnode.buildderefimpl;
  796. begin
  797. inherited buildderefimpl;
  798. if cst_type=cst_ansistring then
  799. astringdefderef.build(astringdef);
  800. end;
  801. procedure tstringconstnode.derefimpl;
  802. begin
  803. inherited derefimpl;
  804. if cst_type=cst_ansistring then
  805. astringdef:=tdef(astringdefderef.resolve);
  806. end;
  807. function tstringconstnode.dogetcopy : tnode;
  808. var
  809. n : tstringconstnode;
  810. begin
  811. n:=tstringconstnode(inherited dogetcopy);
  812. n.cst_type:=cst_type;
  813. n.len:=len;
  814. n.lab_str:=lab_str;
  815. if cst_type in [cst_widestring,cst_unicodestring] then
  816. begin
  817. initwidestring(pcompilerwidestring(n.value_str));
  818. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  819. end
  820. else
  821. n.value_str:=getpcharcopy;
  822. n.astringdef:=astringdef;
  823. dogetcopy:=n;
  824. end;
  825. function tstringconstnode.pass_typecheck:tnode;
  826. begin
  827. result:=nil;
  828. case cst_type of
  829. cst_conststring :
  830. begin
  831. { handle and store as array[0..len-1] of char }
  832. resultdef:=carraydef.create(0,len-1,s32inttype);
  833. tarraydef(resultdef).elementdef:=cansichartype;
  834. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  835. end;
  836. cst_shortstring :
  837. resultdef:=cshortstringtype;
  838. cst_ansistring :
  839. if not assigned(astringdef) then
  840. resultdef:=getansistringdef
  841. else
  842. resultdef:=astringdef;
  843. cst_unicodestring :
  844. resultdef:=cunicodestringtype;
  845. cst_widestring :
  846. resultdef:=cwidestringtype;
  847. cst_longstring :
  848. resultdef:=clongstringtype;
  849. end;
  850. end;
  851. function tstringconstnode.pass_1 : tnode;
  852. begin
  853. result:=nil;
  854. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  855. begin
  856. if len=0 then
  857. expectloc:=LOC_CONSTANT
  858. else
  859. expectloc:=LOC_REGISTER
  860. end
  861. else
  862. expectloc:=LOC_CREFERENCE;
  863. end;
  864. function tstringconstnode.getpcharcopy : pchar;
  865. var
  866. pc : pchar;
  867. begin
  868. pc:=nil;
  869. getmem(pc,len+1);
  870. if pc=nil then
  871. Message(general_f_no_memory_left);
  872. move(value_str^,pc^,len+1);
  873. getpcharcopy:=pc;
  874. end;
  875. function tstringconstnode.docompare(p: tnode): boolean;
  876. begin
  877. docompare :=
  878. inherited docompare(p) and
  879. (len = tstringconstnode(p).len) and
  880. (lab_str = tstringconstnode(p).lab_str) and
  881. { This is enough as soon as labels are allocated, otherwise }
  882. { fall back to content compare. }
  883. (assigned(lab_str) or
  884. (cst_type = tstringconstnode(p).cst_type) and
  885. (fullcompare(tstringconstnode(p)) = 0))
  886. ;
  887. end;
  888. procedure tstringconstnode.changestringtype(def:tdef);
  889. const
  890. st2cst : array[tstringtype] of tconststringtype = (
  891. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  892. var
  893. pw : pcompilerwidestring;
  894. pc : pchar;
  895. cp1 : tstringencoding;
  896. cp2 : tstringencoding;
  897. l,l2 : longint;
  898. begin
  899. if def.typ<>stringdef then
  900. internalerror(200510011);
  901. { convert ascii 2 unicode }
  902. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  903. not(cst_type in [cst_widestring,cst_unicodestring]) then
  904. begin
  905. initwidestring(pw);
  906. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  907. ansistringdispose(value_str,len);
  908. pcompilerwidestring(value_str):=pw;
  909. end
  910. else
  911. { convert unicode 2 ascii }
  912. if (cst_type in [cst_widestring,cst_unicodestring]) and
  913. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  914. begin
  915. cp1:=tstringdef(def).encoding;
  916. if (cp1=globals.CP_NONE) or (cp1=0) then
  917. cp1:=current_settings.sourcecodepage;
  918. if (cp1=CP_UTF8) then
  919. begin
  920. pw:=pcompilerwidestring(value_str);
  921. l2:=len;
  922. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  923. getmem(pc,l);
  924. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  925. len:=l-1;
  926. donewidestring(pw);
  927. value_str:=pc;
  928. end
  929. else
  930. begin
  931. pw:=pcompilerwidestring(value_str);
  932. getmem(pc,getlengthwidestring(pw)+1);
  933. unicode2ascii(pw,pc,cp1);
  934. donewidestring(pw);
  935. value_str:=pc;
  936. end;
  937. end
  938. else
  939. if (tstringdef(def).stringtype = st_ansistring) and
  940. not(cst_type in [cst_widestring,cst_unicodestring]) then
  941. begin
  942. cp1:=tstringdef(def).encoding;
  943. if cp1=0 then
  944. cp1:=current_settings.sourcecodepage;
  945. if (cst_type = cst_ansistring) then
  946. begin
  947. cp2:=tstringdef(resultdef).encoding;
  948. if cp2=0 then
  949. cp2:=current_settings.sourcecodepage;
  950. end
  951. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  952. cp2:=current_settings.sourcecodepage
  953. else
  954. internalerror(2013112916);
  955. { don't change string if codepages are equal or string length is 0 }
  956. if (cp1<>cp2) and (len>0) then
  957. begin
  958. if cpavailable(cp1) and cpavailable(cp2) then
  959. changecodepage(value_str,len,cp2,value_str,cp1)
  960. else if (cp1 <> globals.CP_NONE) and (cp2 <> globals.CP_NONE) then
  961. begin
  962. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  963. if (cp2=CP_UTF8) then
  964. begin
  965. if not cpavailable(cp1) then
  966. begin
  967. Message1(option_code_page_not_available,IntToStr(cp1));
  968. exit;
  969. end;
  970. initwidestring(pw);
  971. setlengthwidestring(pw,len);
  972. { returns room for terminating 0 }
  973. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  974. if (l<>getlengthwidestring(pw)) then
  975. begin
  976. setlengthwidestring(pw,l);
  977. ReAllocMem(value_str,l);
  978. end;
  979. unicode2ascii(pw,value_str,cp1);
  980. len:=l-1;
  981. donewidestring(pw);
  982. end
  983. else
  984. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  985. if (cp1=CP_UTF8) then
  986. begin
  987. if not cpavailable(cp2) then
  988. begin
  989. Message1(option_code_page_not_available,IntToStr(cp2));
  990. exit;
  991. end;
  992. initwidestring(pw);
  993. setlengthwidestring(pw,len);
  994. ascii2unicode(value_str,len,cp2,pw);
  995. { returns room for terminating 0 }
  996. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  997. if l<>len then
  998. ReAllocMem(value_str,l);
  999. len:=l-1;
  1000. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  1001. donewidestring(pw);
  1002. end
  1003. else
  1004. begin
  1005. { output error message that encoding is not available for the compiler }
  1006. if not cpavailable(cp1) then
  1007. Message1(option_code_page_not_available,IntToStr(cp1));
  1008. if not cpavailable(cp2) then
  1009. Message1(option_code_page_not_available,IntToStr(cp2));
  1010. end;
  1011. end;
  1012. end;
  1013. end;
  1014. cst_type:=st2cst[tstringdef(def).stringtype];
  1015. resultdef:=def;
  1016. end;
  1017. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  1018. begin
  1019. if cst_type<>p.cst_type then
  1020. InternalError(2009121701);
  1021. if cst_type in [cst_widestring,cst_unicodestring] then
  1022. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  1023. else
  1024. result:=compareansistrings(value_str,p.value_str,len,p.len);
  1025. end;
  1026. function tstringconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  1027. var
  1028. ss : shortstring;
  1029. labofs : tasmlabofs;
  1030. winlikewidestring : boolean;
  1031. begin
  1032. case tstringdef(resultdef).stringtype of
  1033. st_shortstring:
  1034. begin
  1035. setlength(ss,len);
  1036. move(value_str^,ss[1],len);
  1037. tcb.emit_shortstring_const(ss);
  1038. result:=len+1;
  1039. end;
  1040. st_longstring:
  1041. internalerror(2019070801);
  1042. st_ansistring:
  1043. begin
  1044. labofs:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  1045. tcb.emit_string_offset(labofs,len,tstringdef(resultdef).stringtype,false,charpointertype);
  1046. result:=voidpointertype.size;
  1047. end;
  1048. st_widestring,
  1049. st_unicodestring:
  1050. begin
  1051. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  1052. labofs:=tcb.emit_unicodestring_const(current_asmdata.asmlists[al_typedconsts],value_str,tstringdef(resultdef).encoding,winlikewidestring);
  1053. tcb.emit_string_offset(labofs,len,tstringdef(resultdef).stringtype,false,widecharpointertype);
  1054. result:=voidpointertype.size;
  1055. end;
  1056. end;
  1057. end;
  1058. class function tstringconstnode.emptydynstrnil: boolean;
  1059. begin
  1060. result:=true;
  1061. end;
  1062. {$ifdef DEBUG_NODE_XML}
  1063. procedure TStringConstNode.XMLPrintNodeData(var T: Text);
  1064. var
  1065. OutputStr: ansistring;
  1066. begin
  1067. inherited XMLPrintNodeData(T);
  1068. Write(T, printnodeindention, '<stringtype>');
  1069. case cst_type of
  1070. cst_conststring:
  1071. Write(T, 'conststring');
  1072. cst_shortstring:
  1073. Write(T, 'shortstring');
  1074. cst_longstring:
  1075. Write(T, 'longstring');
  1076. cst_ansistring:
  1077. Write(T, 'ansistring');
  1078. cst_widestring:
  1079. Write(T, 'widestring');
  1080. cst_unicodestring:
  1081. Write(T, 'unicodestring');
  1082. end;
  1083. WriteLn(T, '</stringtype>');
  1084. WriteLn(T, printnodeindention, '<length>', len, '</length>');
  1085. if len = 0 then
  1086. begin
  1087. WriteLn(T, printnodeindention, '<value />');
  1088. Exit;
  1089. end;
  1090. case cst_type of
  1091. cst_widestring, cst_unicodestring:
  1092. begin
  1093. { value_str is of type PCompilerWideString }
  1094. SetLength(OutputStr, len);
  1095. UnicodeToUtf8(PChar(OutputStr), PUnicodeChar(PCompilerWideString(value_str)^.data), len + 1); { +1 for the null terminator }
  1096. end;
  1097. else
  1098. OutputStr := ansistring(value_str);
  1099. SetLength(OutputStr, len);
  1100. end;
  1101. WriteLn(T, printnodeindention, '<value>', SanitiseXMLString(OutputStr), '</value>');
  1102. end;
  1103. {$endif DEBUG_NODE_XML}
  1104. {*****************************************************************************
  1105. TSETCONSTNODE
  1106. *****************************************************************************}
  1107. constructor tsetconstnode.create(s : pconstset;def:tdef);
  1108. begin
  1109. inherited create(setconstn,nil);
  1110. typedef:=def;
  1111. if assigned(s) then
  1112. begin
  1113. new(value_set);
  1114. value_set^:=s^;
  1115. end
  1116. else
  1117. value_set:=nil;
  1118. end;
  1119. destructor tsetconstnode.destroy;
  1120. begin
  1121. if assigned(value_set) then
  1122. dispose(value_set);
  1123. inherited destroy;
  1124. end;
  1125. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1126. begin
  1127. inherited ppuload(t,ppufile);
  1128. ppufile.getderef(typedefderef);
  1129. new(value_set);
  1130. ppufile.getset(tppuset32(value_set^));
  1131. end;
  1132. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1133. begin
  1134. inherited ppuwrite(ppufile);
  1135. ppufile.putderef(typedefderef);
  1136. ppufile.putset(tppuset32(value_set^));
  1137. end;
  1138. procedure tsetconstnode.buildderefimpl;
  1139. begin
  1140. inherited buildderefimpl;
  1141. typedefderef.build(typedef);
  1142. end;
  1143. procedure tsetconstnode.derefimpl;
  1144. begin
  1145. inherited derefimpl;
  1146. typedef:=tdef(typedefderef.resolve);
  1147. end;
  1148. type
  1149. setbytes = array[0..31] of byte;
  1150. Psetbytes = ^setbytes;
  1151. procedure tsetconstnode.adjustforsetbase;
  1152. var
  1153. i, diff: longint;
  1154. begin
  1155. { Internally, the compiler stores all sets with setbase 0, so we have }
  1156. { to convert the set to its actual format in case setbase<>0 when }
  1157. { writing it out }
  1158. if (tsetdef(resultdef).setbase<>0) then
  1159. begin
  1160. if (tsetdef(resultdef).setbase and 7)<>0 then
  1161. internalerror(2007091501);
  1162. diff:=tsetdef(resultdef).setbase div 8;
  1163. { This is endian-neutral in the new set format: in both cases, }
  1164. { the first byte contains the first elements of the set. }
  1165. { Since the compiler/base rtl cannot contain packed sets before }
  1166. { they work for big endian, it's no problem that the code below }
  1167. { is wrong for the old big endian set format (setbase cannot be }
  1168. { <>0 with non-packed sets). }
  1169. for i:=0 to tsetdef(resultdef).size-1 do
  1170. begin
  1171. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1172. Psetbytes(value_set)^[i+diff]:=0;
  1173. end;
  1174. end;
  1175. end;
  1176. function tsetconstnode.dogetcopy : tnode;
  1177. var
  1178. n : tsetconstnode;
  1179. begin
  1180. n:=tsetconstnode(inherited dogetcopy);
  1181. if assigned(value_set) then
  1182. begin
  1183. new(n.value_set);
  1184. n.value_set^:=value_set^
  1185. end
  1186. else
  1187. n.value_set:=nil;
  1188. n.typedef := typedef;
  1189. n.lab_set:=lab_set;
  1190. dogetcopy:=n;
  1191. end;
  1192. function tsetconstnode.pass_typecheck:tnode;
  1193. begin
  1194. result:=nil;
  1195. resultdef:=typedef;
  1196. end;
  1197. function tsetconstnode.pass_1 : tnode;
  1198. begin
  1199. result:=nil;
  1200. if is_smallset(resultdef) then
  1201. expectloc:=LOC_CONSTANT
  1202. else
  1203. expectloc:=LOC_CREFERENCE;
  1204. end;
  1205. function tsetconstnode.docompare(p: tnode): boolean;
  1206. begin
  1207. docompare:=(inherited docompare(p)) and
  1208. (value_set^=Tsetconstnode(p).value_set^);
  1209. end;
  1210. function tsetconstnode.elements : AInt;
  1211. var
  1212. i : longint;
  1213. begin
  1214. result:=0;
  1215. if not(assigned(value_set)) then
  1216. exit;
  1217. for i:=0 to tsetdef(resultdef).size-1 do
  1218. result:=result+ PopCnt(Psetbytes(value_set)^[i]);
  1219. end;
  1220. function tsetconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  1221. type
  1222. setbytes=array[0..31] of byte;
  1223. Psetbytes=^setbytes;
  1224. var
  1225. setval : aint;
  1226. i : sizeint;
  1227. begin
  1228. if is_smallset(resultdef) then
  1229. begin
  1230. if (source_info.endian=target_info.endian) then
  1231. begin
  1232. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  1233. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  1234. { cross-endian code below }
  1235. { Extra aint type cast to avoid range errors }
  1236. setval:=aint(pCardinal(value_set)^)
  1237. end
  1238. else
  1239. begin
  1240. setval:=aint(swapendian(Pcardinal(value_set)^));
  1241. setval:=aint(
  1242. reverse_byte (setval and $ff) or
  1243. (reverse_byte((setval shr 8) and $ff) shl 8) or
  1244. (reverse_byte((setval shr 16) and $ff) shl 16) or
  1245. (reverse_byte((setval shr 24) and $ff) shl 24)
  1246. );
  1247. end;
  1248. if (target_info.endian=endian_big) then
  1249. setval:=setval shr (32-resultdef.size*8);
  1250. case resultdef.size of
  1251. 1:
  1252. tcb.emit_ord_const(byte(setval),u8inttype);
  1253. 2:
  1254. tcb.emit_ord_const(word(setval),u16inttype);
  1255. 4:
  1256. tcb.emit_ord_const(longword(setval),u32inttype);
  1257. 8:
  1258. tcb.emit_ord_const(qword(setval),u64inttype);
  1259. else
  1260. internalerror(2019070802);
  1261. end;
  1262. end
  1263. else
  1264. begin
  1265. if (source_info.endian=target_info.endian) then
  1266. for i:=0 to resultdef.size-1 do
  1267. tcb.emit_tai(tai_const.create_8bit(Psetbytes(value_set)^[i]),u8inttype)
  1268. else
  1269. for i:=0 to resultdef.size-1 do
  1270. tcb.emit_tai(tai_const.create_8bit(reverse_byte(Psetbytes(value_set)^[i])),u8inttype);
  1271. end;
  1272. result:=resultdef.size;
  1273. end;
  1274. {*****************************************************************************
  1275. TNILNODE
  1276. *****************************************************************************}
  1277. constructor tnilnode.create;
  1278. begin
  1279. inherited create(niln);
  1280. end;
  1281. function tnilnode.pass_typecheck:tnode;
  1282. begin
  1283. result:=nil;
  1284. resultdef:=voidpointertype;
  1285. end;
  1286. function tnilnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  1287. begin
  1288. if tpointerdef(resultdef).compatible_with_pointerdef_size(tpointerdef(voidpointertype)) then
  1289. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype)
  1290. else
  1291. tcb.emit_tai(tai_const.Create_nil_codeptr,voidcodepointertype);
  1292. result:=resultdef.size;
  1293. end;
  1294. function tnilnode.pass_1 : tnode;
  1295. begin
  1296. result:=nil;
  1297. expectloc:=LOC_CONSTANT;
  1298. end;
  1299. {*****************************************************************************
  1300. TGUIDCONSTNODE
  1301. *****************************************************************************}
  1302. constructor tguidconstnode.create(const g:tguid);
  1303. begin
  1304. inherited create(guidconstn);
  1305. value:=g;
  1306. end;
  1307. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1308. begin
  1309. inherited ppuload(t,ppufile);
  1310. ppufile.getguid(value);
  1311. end;
  1312. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1313. begin
  1314. inherited ppuwrite(ppufile);
  1315. ppufile.putguid(value);
  1316. end;
  1317. function tguidconstnode.dogetcopy : tnode;
  1318. var
  1319. n : tguidconstnode;
  1320. begin
  1321. n:=tguidconstnode(inherited dogetcopy);
  1322. n.value:=value;
  1323. n.lab_set:=lab_set;
  1324. dogetcopy:=n;
  1325. end;
  1326. function tguidconstnode.pass_typecheck:tnode;
  1327. begin
  1328. result:=nil;
  1329. resultdef:=rec_tguid;
  1330. end;
  1331. function tguidconstnode.pass_1 : tnode;
  1332. begin
  1333. result:=nil;
  1334. expectloc:=LOC_CREFERENCE;
  1335. end;
  1336. function tguidconstnode.docompare(p: tnode): boolean;
  1337. begin
  1338. docompare :=
  1339. inherited docompare(p) and
  1340. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1341. end;
  1342. function tguidconstnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  1343. begin
  1344. tcb.emit_guid_const(value);
  1345. result:=resultdef.size;
  1346. end;
  1347. end.