ncon.pas 53 KB

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