ncon.pas 51 KB

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