ncon.pas 52 KB

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