ncon.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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]) 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,'value = ',tostr(value));
  602. end;
  603. function tordconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  604. begin
  605. tcb.emit_ord_const(value,resultdef);
  606. result:=resultdef.size;
  607. end;
  608. {$ifdef DEBUG_NODE_XML}
  609. procedure TOrdConstNode.XMLPrintNodeInfo(var T: Text);
  610. begin
  611. inherited XMLPrintNodeInfo(T);
  612. Write(T, ' rangecheck="', rangecheck, '"');
  613. end;
  614. procedure TOrdConstNode.XMLPrintNodeData(var T: Text);
  615. begin
  616. inherited XMLPrintNodeData(T);
  617. WriteLn(T, printnodeindention, '<value>', tostr(value), '</value>');
  618. end;
  619. {$endif DEBUG_NODE_XML}
  620. {*****************************************************************************
  621. TPOINTERCONSTNODE
  622. *****************************************************************************}
  623. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  624. begin
  625. inherited create(pointerconstn);
  626. value:=v;
  627. typedef:=def;
  628. end;
  629. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  630. begin
  631. inherited ppuload(t,ppufile);
  632. ppufile.getderef(typedefderef);
  633. value:=ppufile.getptruint;
  634. end;
  635. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  636. begin
  637. inherited ppuwrite(ppufile);
  638. ppufile.putderef(typedefderef);
  639. ppufile.putptruint(value);
  640. end;
  641. procedure tpointerconstnode.buildderefimpl;
  642. begin
  643. inherited buildderefimpl;
  644. typedefderef.build(typedef);
  645. end;
  646. procedure tpointerconstnode.derefimpl;
  647. begin
  648. inherited derefimpl;
  649. typedef:=tdef(typedefderef.resolve);
  650. end;
  651. function tpointerconstnode.dogetcopy : tnode;
  652. var
  653. n : tpointerconstnode;
  654. begin
  655. n:=tpointerconstnode(inherited dogetcopy);
  656. n.value:=value;
  657. n.typedef := typedef;
  658. dogetcopy:=n;
  659. end;
  660. function tpointerconstnode.pass_typecheck:tnode;
  661. begin
  662. result:=nil;
  663. resultdef:=typedef;
  664. end;
  665. function tpointerconstnode.pass_1 : tnode;
  666. begin
  667. result:=nil;
  668. expectloc:=LOC_CONSTANT;
  669. end;
  670. function tpointerconstnode.docompare(p: tnode): boolean;
  671. begin
  672. docompare :=
  673. inherited docompare(p) and
  674. (value = tpointerconstnode(p).value);
  675. end;
  676. procedure tpointerconstnode.printnodedata(var t : text);
  677. begin
  678. inherited printnodedata(t);
  679. writeln(t,printnodeindention,'value = $',hexstr(PUInt(value),sizeof(PUInt)*2));
  680. end;
  681. function tpointerconstnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  682. begin
  683. if tpointerdef(resultdef).compatible_with_pointerdef_size(tpointerdef(voidpointertype)) then
  684. tcb.emit_tai(tai_const.Create_int_dataptr(value),voidpointertype)
  685. else
  686. tcb.emit_tai(tai_const.Create_int_codeptr(value),voidcodepointertype);
  687. result:=resultdef.size;
  688. end;
  689. {$ifdef DEBUG_NODE_XML}
  690. procedure TPointerConstNode.XMLPrintNodeData(var T: Text);
  691. begin
  692. inherited XMLPrintNodeData(T);
  693. WriteLn(T, PrintNodeIndention, '<value>$', hexstr(PUInt(value),sizeof(PUInt)*2), '</value>');
  694. end;
  695. {$endif DEBUG_NODE_XML}
  696. {*****************************************************************************
  697. TSTRINGCONSTNODE
  698. *****************************************************************************}
  699. constructor tstringconstnode.createstr(const s : string);
  700. var
  701. l : longint;
  702. begin
  703. inherited create(stringconstn);
  704. l:=length(s);
  705. len:=l;
  706. { stringdup write even past a #0 }
  707. getmem(value_str,l+1);
  708. move(s[1],value_str^,l);
  709. value_str[l]:=#0;
  710. lab_str:=nil;
  711. cst_type:=cst_conststring;
  712. end;
  713. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  714. begin
  715. inherited create(stringconstn);
  716. len:=getlengthwidestring(w);
  717. initwidestring(pcompilerwidestring(value_str));
  718. copywidestring(w,pcompilerwidestring(value_str));
  719. lab_str:=nil;
  720. cst_type:=cst_unicodestring;
  721. end;
  722. constructor tstringconstnode.createpchar(s: pchar; l: longint; def: tdef);
  723. begin
  724. inherited create(stringconstn);
  725. len:=l;
  726. value_str:=s;
  727. if assigned(def) and
  728. is_ansistring(def) then
  729. begin
  730. cst_type:=cst_ansistring;
  731. astringdef:=def;
  732. end
  733. else
  734. cst_type:=cst_conststring;
  735. lab_str:=nil;
  736. end;
  737. destructor tstringconstnode.destroy;
  738. begin
  739. if cst_type in [cst_widestring,cst_unicodestring] then
  740. donewidestring(pcompilerwidestring(value_str))
  741. else
  742. ansistringdispose(value_str,len);
  743. inherited destroy;
  744. end;
  745. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  746. var
  747. pw : pcompilerwidestring;
  748. i : longint;
  749. begin
  750. inherited ppuload(t,ppufile);
  751. cst_type:=tconststringtype(ppufile.getbyte);
  752. len:=ppufile.getlongint;
  753. if cst_type in [cst_widestring,cst_unicodestring] then
  754. begin
  755. initwidestring(pw);
  756. setlengthwidestring(pw,len);
  757. { don't use getdata, because the compilerwidechars may have to
  758. be byteswapped
  759. }
  760. {$if sizeof(tcompilerwidechar) = 2}
  761. for i:=0 to pw^.len-1 do
  762. pw^.data[i]:=ppufile.getword;
  763. {$elseif sizeof(tcompilerwidechar) = 4}
  764. for i:=0 to pw^.len-1 do
  765. pw^.data[i]:=cardinal(ppufile.getlongint);
  766. {$else}
  767. {$error Unsupported tcompilerwidechar size}
  768. {$endif}
  769. pcompilerwidestring(value_str):=pw
  770. end
  771. else
  772. begin
  773. getmem(value_str,len+1);
  774. ppufile.getdata(value_str^,len);
  775. value_str[len]:=#0;
  776. end;
  777. lab_str:=tasmlabel(ppufile.getasmsymbol);
  778. if cst_type=cst_ansistring then
  779. ppufile.getderef(astringdefderef);
  780. end;
  781. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  782. begin
  783. inherited ppuwrite(ppufile);
  784. ppufile.putbyte(byte(cst_type));
  785. ppufile.putlongint(len);
  786. if cst_type in [cst_widestring,cst_unicodestring] then
  787. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  788. else
  789. ppufile.putdata(value_str^,len);
  790. ppufile.putasmsymbol(lab_str);
  791. if cst_type=cst_ansistring then
  792. ppufile.putderef(astringdefderef);
  793. end;
  794. procedure tstringconstnode.buildderefimpl;
  795. begin
  796. inherited buildderefimpl;
  797. if cst_type=cst_ansistring then
  798. astringdefderef.build(astringdef);
  799. end;
  800. procedure tstringconstnode.derefimpl;
  801. begin
  802. inherited derefimpl;
  803. if cst_type=cst_ansistring then
  804. astringdef:=tdef(astringdefderef.resolve);
  805. end;
  806. function tstringconstnode.dogetcopy : tnode;
  807. var
  808. n : tstringconstnode;
  809. begin
  810. n:=tstringconstnode(inherited dogetcopy);
  811. n.cst_type:=cst_type;
  812. n.len:=len;
  813. n.lab_str:=lab_str;
  814. if cst_type in [cst_widestring,cst_unicodestring] then
  815. begin
  816. initwidestring(pcompilerwidestring(n.value_str));
  817. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  818. end
  819. else
  820. n.value_str:=getpcharcopy;
  821. n.astringdef:=astringdef;
  822. dogetcopy:=n;
  823. end;
  824. function tstringconstnode.pass_typecheck:tnode;
  825. var
  826. l : aint;
  827. begin
  828. result:=nil;
  829. case cst_type of
  830. cst_conststring :
  831. begin
  832. { handle and store as array[0..len-1] of char }
  833. if len>0 then
  834. l:=len-1
  835. else
  836. l:=0;
  837. resultdef:=carraydef.create(0,l,s32inttype);
  838. tarraydef(resultdef).elementdef:=cansichartype;
  839. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  840. end;
  841. cst_shortstring :
  842. resultdef:=cshortstringtype;
  843. cst_ansistring :
  844. if not assigned(astringdef) then
  845. resultdef:=getansistringdef
  846. else
  847. resultdef:=astringdef;
  848. cst_unicodestring :
  849. resultdef:=cunicodestringtype;
  850. cst_widestring :
  851. resultdef:=cwidestringtype;
  852. cst_longstring :
  853. resultdef:=clongstringtype;
  854. end;
  855. end;
  856. function tstringconstnode.pass_1 : tnode;
  857. begin
  858. result:=nil;
  859. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  860. begin
  861. if len=0 then
  862. expectloc:=LOC_CONSTANT
  863. else
  864. expectloc:=LOC_REGISTER
  865. end
  866. else
  867. expectloc:=LOC_CREFERENCE;
  868. end;
  869. function tstringconstnode.getpcharcopy : pchar;
  870. var
  871. pc : pchar;
  872. begin
  873. pc:=nil;
  874. getmem(pc,len+1);
  875. if pc=nil then
  876. Message(general_f_no_memory_left);
  877. move(value_str^,pc^,len+1);
  878. getpcharcopy:=pc;
  879. end;
  880. function tstringconstnode.docompare(p: tnode): boolean;
  881. begin
  882. docompare :=
  883. inherited docompare(p) and
  884. (len = tstringconstnode(p).len) and
  885. (lab_str = tstringconstnode(p).lab_str) and
  886. { This is enough as soon as labels are allocated, otherwise }
  887. { fall back to content compare. }
  888. (assigned(lab_str) or
  889. (cst_type = tstringconstnode(p).cst_type) and
  890. (fullcompare(tstringconstnode(p)) = 0))
  891. ;
  892. end;
  893. procedure tstringconstnode.changestringtype(def:tdef);
  894. const
  895. st2cst : array[tstringtype] of tconststringtype = (
  896. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  897. var
  898. pw : pcompilerwidestring;
  899. pc : pchar;
  900. cp1 : tstringencoding;
  901. cp2 : tstringencoding;
  902. l,l2 : longint;
  903. begin
  904. if def.typ<>stringdef then
  905. internalerror(200510011);
  906. { convert ascii 2 unicode }
  907. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  908. not(cst_type in [cst_widestring,cst_unicodestring]) then
  909. begin
  910. initwidestring(pw);
  911. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  912. ansistringdispose(value_str,len);
  913. pcompilerwidestring(value_str):=pw;
  914. end
  915. else
  916. { convert unicode 2 ascii }
  917. if (cst_type in [cst_widestring,cst_unicodestring]) and
  918. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  919. begin
  920. cp1:=tstringdef(def).encoding;
  921. if (cp1=globals.CP_NONE) or (cp1=0) then
  922. cp1:=current_settings.sourcecodepage;
  923. if (cp1=CP_UTF8) then
  924. begin
  925. pw:=pcompilerwidestring(value_str);
  926. l2:=len;
  927. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  928. getmem(pc,l);
  929. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  930. len:=l-1;
  931. donewidestring(pw);
  932. value_str:=pc;
  933. end
  934. else
  935. begin
  936. pw:=pcompilerwidestring(value_str);
  937. getmem(pc,getlengthwidestring(pw)+1);
  938. unicode2ascii(pw,pc,cp1);
  939. donewidestring(pw);
  940. value_str:=pc;
  941. end;
  942. end
  943. else
  944. if (tstringdef(def).stringtype = st_ansistring) and
  945. not(cst_type in [cst_widestring,cst_unicodestring]) then
  946. begin
  947. cp1:=tstringdef(def).encoding;
  948. if cp1=0 then
  949. cp1:=current_settings.sourcecodepage;
  950. if (cst_type = cst_ansistring) then
  951. begin
  952. cp2:=tstringdef(resultdef).encoding;
  953. if cp2=0 then
  954. cp2:=current_settings.sourcecodepage;
  955. end
  956. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  957. cp2:=current_settings.sourcecodepage
  958. else
  959. internalerror(2013112916);
  960. { don't change string if codepages are equal or string length is 0 }
  961. if (cp1<>cp2) and (len>0) then
  962. begin
  963. if cpavailable(cp1) and cpavailable(cp2) then
  964. changecodepage(value_str,len,cp2,value_str,cp1)
  965. else if (cp1 <> globals.CP_NONE) and (cp2 <> globals.CP_NONE) then
  966. begin
  967. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  968. if (cp2=CP_UTF8) then
  969. begin
  970. if not cpavailable(cp1) then
  971. begin
  972. Message1(option_code_page_not_available,IntToStr(cp1));
  973. exit;
  974. end;
  975. initwidestring(pw);
  976. setlengthwidestring(pw,len);
  977. { returns room for terminating 0 }
  978. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  979. if (l<>getlengthwidestring(pw)) then
  980. begin
  981. setlengthwidestring(pw,l);
  982. ReAllocMem(value_str,l);
  983. end;
  984. unicode2ascii(pw,value_str,cp1);
  985. len:=l-1;
  986. donewidestring(pw);
  987. end
  988. else
  989. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  990. if (cp1=CP_UTF8) then
  991. begin
  992. if not cpavailable(cp2) then
  993. begin
  994. Message1(option_code_page_not_available,IntToStr(cp2));
  995. exit;
  996. end;
  997. initwidestring(pw);
  998. setlengthwidestring(pw,len);
  999. ascii2unicode(value_str,len,cp2,pw);
  1000. { returns room for terminating 0 }
  1001. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  1002. if l<>len then
  1003. ReAllocMem(value_str,l);
  1004. len:=l-1;
  1005. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  1006. donewidestring(pw);
  1007. end
  1008. else
  1009. begin
  1010. { output error message that encoding is not available for the compiler }
  1011. if not cpavailable(cp1) then
  1012. Message1(option_code_page_not_available,IntToStr(cp1));
  1013. if not cpavailable(cp2) then
  1014. Message1(option_code_page_not_available,IntToStr(cp2));
  1015. end;
  1016. end;
  1017. end;
  1018. end;
  1019. cst_type:=st2cst[tstringdef(def).stringtype];
  1020. resultdef:=def;
  1021. end;
  1022. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  1023. begin
  1024. if cst_type<>p.cst_type then
  1025. InternalError(2009121701);
  1026. if cst_type in [cst_widestring,cst_unicodestring] then
  1027. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  1028. else
  1029. result:=compareansistrings(value_str,p.value_str,len,p.len);
  1030. end;
  1031. function tstringconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  1032. var
  1033. ss : shortstring;
  1034. labofs : tasmlabofs;
  1035. winlikewidestring : boolean;
  1036. begin
  1037. case tstringdef(resultdef).stringtype of
  1038. st_shortstring:
  1039. begin
  1040. setlength(ss,len);
  1041. move(value_str^,ss[1],len);
  1042. tcb.emit_shortstring_const(ss);
  1043. result:=len+1;
  1044. end;
  1045. st_longstring:
  1046. internalerror(2019070801);
  1047. st_ansistring:
  1048. begin
  1049. labofs:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  1050. tcb.emit_string_offset(labofs,len,tstringdef(resultdef).stringtype,false,charpointertype);
  1051. result:=voidpointertype.size;
  1052. end;
  1053. st_widestring,
  1054. st_unicodestring:
  1055. begin
  1056. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  1057. labofs:=tcb.emit_unicodestring_const(current_asmdata.asmlists[al_typedconsts],value_str,tstringdef(resultdef).encoding,winlikewidestring);
  1058. tcb.emit_string_offset(labofs,len,tstringdef(resultdef).stringtype,false,widecharpointertype);
  1059. result:=voidpointertype.size;
  1060. end;
  1061. end;
  1062. end;
  1063. class function tstringconstnode.emptydynstrnil: boolean;
  1064. begin
  1065. result:=true;
  1066. end;
  1067. {$ifdef DEBUG_NODE_XML}
  1068. procedure TStringConstNode.XMLPrintNodeData(var T: Text);
  1069. var
  1070. OutputStr: ansistring;
  1071. begin
  1072. inherited XMLPrintNodeData(T);
  1073. Write(T, printnodeindention, '<stringtype>');
  1074. case cst_type of
  1075. cst_conststring:
  1076. Write(T, 'conststring');
  1077. cst_shortstring:
  1078. Write(T, 'shortstring');
  1079. cst_longstring:
  1080. Write(T, 'longstring');
  1081. cst_ansistring:
  1082. Write(T, 'ansistring');
  1083. cst_widestring:
  1084. Write(T, 'widestring');
  1085. cst_unicodestring:
  1086. Write(T, 'unicodestring');
  1087. end;
  1088. WriteLn(T, '</stringtype>');
  1089. WriteLn(T, printnodeindention, '<length>', len, '</length>');
  1090. if len = 0 then
  1091. begin
  1092. WriteLn(T, printnodeindention, '<value />');
  1093. Exit;
  1094. end;
  1095. case cst_type of
  1096. cst_widestring, cst_unicodestring:
  1097. begin
  1098. { value_str is of type PCompilerWideString }
  1099. SetLength(OutputStr, len);
  1100. UnicodeToUtf8(PChar(OutputStr), PUnicodeChar(PCompilerWideString(value_str)^.data), len + 1); { +1 for the null terminator }
  1101. end;
  1102. else
  1103. OutputStr := ansistring(value_str);
  1104. SetLength(OutputStr, len);
  1105. end;
  1106. WriteLn(T, printnodeindention, '<value>', SanitiseXMLString(OutputStr), '</value>');
  1107. end;
  1108. {$endif DEBUG_NODE_XML}
  1109. {*****************************************************************************
  1110. TSETCONSTNODE
  1111. *****************************************************************************}
  1112. constructor tsetconstnode.create(s : pconstset;def:tdef);
  1113. begin
  1114. inherited create(setconstn,nil);
  1115. typedef:=def;
  1116. if assigned(s) then
  1117. begin
  1118. new(value_set);
  1119. value_set^:=s^;
  1120. end
  1121. else
  1122. value_set:=nil;
  1123. end;
  1124. destructor tsetconstnode.destroy;
  1125. begin
  1126. if assigned(value_set) then
  1127. dispose(value_set);
  1128. inherited destroy;
  1129. end;
  1130. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1131. begin
  1132. inherited ppuload(t,ppufile);
  1133. ppufile.getderef(typedefderef);
  1134. new(value_set);
  1135. ppufile.getset(tppuset32(value_set^));
  1136. end;
  1137. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1138. begin
  1139. inherited ppuwrite(ppufile);
  1140. ppufile.putderef(typedefderef);
  1141. ppufile.putset(tppuset32(value_set^));
  1142. end;
  1143. procedure tsetconstnode.buildderefimpl;
  1144. begin
  1145. inherited buildderefimpl;
  1146. typedefderef.build(typedef);
  1147. end;
  1148. procedure tsetconstnode.derefimpl;
  1149. begin
  1150. inherited derefimpl;
  1151. typedef:=tdef(typedefderef.resolve);
  1152. end;
  1153. type
  1154. setbytes = array[0..31] of byte;
  1155. Psetbytes = ^setbytes;
  1156. procedure tsetconstnode.adjustforsetbase;
  1157. var
  1158. i, diff: longint;
  1159. begin
  1160. { Internally, the compiler stores all sets with setbase 0, so we have }
  1161. { to convert the set to its actual format in case setbase<>0 when }
  1162. { writing it out }
  1163. if (tsetdef(resultdef).setbase<>0) then
  1164. begin
  1165. if (tsetdef(resultdef).setbase and 7)<>0 then
  1166. internalerror(2007091501);
  1167. diff:=tsetdef(resultdef).setbase div 8;
  1168. { This is endian-neutral in the new set format: in both cases, }
  1169. { the first byte contains the first elements of the set. }
  1170. { Since the compiler/base rtl cannot contain packed sets before }
  1171. { they work for big endian, it's no problem that the code below }
  1172. { is wrong for the old big endian set format (setbase cannot be }
  1173. { <>0 with non-packed sets). }
  1174. for i:=0 to tsetdef(resultdef).size-1 do
  1175. begin
  1176. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1177. Psetbytes(value_set)^[i+diff]:=0;
  1178. end;
  1179. end;
  1180. end;
  1181. function tsetconstnode.dogetcopy : tnode;
  1182. var
  1183. n : tsetconstnode;
  1184. begin
  1185. n:=tsetconstnode(inherited dogetcopy);
  1186. if assigned(value_set) then
  1187. begin
  1188. new(n.value_set);
  1189. n.value_set^:=value_set^
  1190. end
  1191. else
  1192. n.value_set:=nil;
  1193. n.typedef := typedef;
  1194. n.lab_set:=lab_set;
  1195. dogetcopy:=n;
  1196. end;
  1197. function tsetconstnode.pass_typecheck:tnode;
  1198. begin
  1199. result:=nil;
  1200. resultdef:=typedef;
  1201. end;
  1202. function tsetconstnode.pass_1 : tnode;
  1203. begin
  1204. result:=nil;
  1205. if is_smallset(resultdef) then
  1206. expectloc:=LOC_CONSTANT
  1207. else
  1208. expectloc:=LOC_CREFERENCE;
  1209. end;
  1210. function tsetconstnode.docompare(p: tnode): boolean;
  1211. begin
  1212. docompare:=(inherited docompare(p)) and
  1213. (value_set^=Tsetconstnode(p).value_set^);
  1214. end;
  1215. function tsetconstnode.elements : AInt;
  1216. var
  1217. i : longint;
  1218. begin
  1219. result:=0;
  1220. if not(assigned(value_set)) then
  1221. exit;
  1222. for i:=0 to tsetdef(resultdef).size-1 do
  1223. result:=result+ PopCnt(Psetbytes(value_set)^[i]);
  1224. end;
  1225. function tsetconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  1226. type
  1227. setbytes=array[0..31] of byte;
  1228. Psetbytes=^setbytes;
  1229. var
  1230. setval : aint;
  1231. i : sizeint;
  1232. begin
  1233. if is_smallset(resultdef) then
  1234. begin
  1235. if (source_info.endian=target_info.endian) then
  1236. begin
  1237. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  1238. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  1239. { cross-endian code below }
  1240. { Extra aint type cast to avoid range errors }
  1241. setval:=aint(pCardinal(value_set)^)
  1242. end
  1243. else
  1244. begin
  1245. setval:=aint(swapendian(Pcardinal(value_set)^));
  1246. setval:=aint(
  1247. reverse_byte (setval and $ff) or
  1248. (reverse_byte((setval shr 8) and $ff) shl 8) or
  1249. (reverse_byte((setval shr 16) and $ff) shl 16) or
  1250. (reverse_byte((setval shr 24) and $ff) shl 24)
  1251. );
  1252. end;
  1253. if (target_info.endian=endian_big) then
  1254. setval:=setval shr (32-resultdef.size*8);
  1255. case resultdef.size of
  1256. 1:
  1257. tcb.emit_ord_const(byte(setval),u8inttype);
  1258. 2:
  1259. tcb.emit_ord_const(word(setval),u16inttype);
  1260. 4:
  1261. tcb.emit_ord_const(longword(setval),u32inttype);
  1262. 8:
  1263. tcb.emit_ord_const(qword(setval),u64inttype);
  1264. else
  1265. internalerror(2019070802);
  1266. end;
  1267. end
  1268. else
  1269. begin
  1270. if (source_info.endian=target_info.endian) then
  1271. for i:=0 to resultdef.size-1 do
  1272. tcb.emit_tai(tai_const.create_8bit(Psetbytes(value_set)^[i]),u8inttype)
  1273. else
  1274. for i:=0 to resultdef.size-1 do
  1275. tcb.emit_tai(tai_const.create_8bit(reverse_byte(Psetbytes(value_set)^[i])),u8inttype);
  1276. end;
  1277. result:=resultdef.size;
  1278. end;
  1279. {*****************************************************************************
  1280. TNILNODE
  1281. *****************************************************************************}
  1282. constructor tnilnode.create;
  1283. begin
  1284. inherited create(niln);
  1285. end;
  1286. function tnilnode.pass_typecheck:tnode;
  1287. begin
  1288. result:=nil;
  1289. resultdef:=voidpointertype;
  1290. end;
  1291. function tnilnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  1292. begin
  1293. if tpointerdef(resultdef).compatible_with_pointerdef_size(tpointerdef(voidpointertype)) then
  1294. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype)
  1295. else
  1296. tcb.emit_tai(tai_const.Create_nil_codeptr,voidcodepointertype);
  1297. result:=resultdef.size;
  1298. end;
  1299. function tnilnode.pass_1 : tnode;
  1300. begin
  1301. result:=nil;
  1302. expectloc:=LOC_CONSTANT;
  1303. end;
  1304. {*****************************************************************************
  1305. TGUIDCONSTNODE
  1306. *****************************************************************************}
  1307. constructor tguidconstnode.create(const g:tguid);
  1308. begin
  1309. inherited create(guidconstn);
  1310. value:=g;
  1311. end;
  1312. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1313. begin
  1314. inherited ppuload(t,ppufile);
  1315. ppufile.getguid(value);
  1316. end;
  1317. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1318. begin
  1319. inherited ppuwrite(ppufile);
  1320. ppufile.putguid(value);
  1321. end;
  1322. function tguidconstnode.dogetcopy : tnode;
  1323. var
  1324. n : tguidconstnode;
  1325. begin
  1326. n:=tguidconstnode(inherited dogetcopy);
  1327. n.value:=value;
  1328. n.lab_set:=lab_set;
  1329. dogetcopy:=n;
  1330. end;
  1331. function tguidconstnode.pass_typecheck:tnode;
  1332. begin
  1333. result:=nil;
  1334. resultdef:=rec_tguid;
  1335. end;
  1336. function tguidconstnode.pass_1 : tnode;
  1337. begin
  1338. result:=nil;
  1339. expectloc:=LOC_CREFERENCE;
  1340. end;
  1341. function tguidconstnode.docompare(p: tnode): boolean;
  1342. begin
  1343. docompare :=
  1344. inherited docompare(p) and
  1345. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1346. end;
  1347. function tguidconstnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  1348. begin
  1349. tcb.emit_guid_const(value);
  1350. result:=resultdef.size;
  1351. end;
  1352. end.