2
0

ncon.pas 51 KB

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