ncon.pas 49 KB

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