ncon.pas 53 KB

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