ncon.pas 53 KB

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