ncon.pas 53 KB

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