ncon.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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,ppu,
  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. write(t,printnodeindention,'value = ',value_real);
  465. if is_currency(resultdef) then
  466. writeln(', value_currency = ',value_currency)
  467. else
  468. writeln;
  469. end;
  470. function trealconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  471. begin
  472. case tfloatdef(typedef).floattype of
  473. s32real:
  474. tcb.emit_tai(tai_realconst.create_s32real(value_real),typedef);
  475. s64real:
  476. tcb.emit_tai(tai_realconst.create_s64real(value_real),typedef);
  477. s80real:
  478. tcb.emit_tai(tai_realconst.create_s80real(value_real,s80floattype.size),typedef);
  479. sc80real:
  480. tcb.emit_tai(tai_realconst.create_s80real(value_real,sc80floattype.size),typedef);
  481. s64comp:
  482. { the round is necessary for native compilers where comp isn't a float }
  483. tcb.emit_tai(tai_realconst.create_s64compreal(round(value_real)),typedef);
  484. s64currency:
  485. { we don't need to multiply by 10000 here }
  486. tcb.emit_tai(tai_realconst.create_s64compreal(round(value_real)),typedef);
  487. s128real:
  488. internalerror(2019070804);
  489. end;
  490. result:=resultdef.size;
  491. end;
  492. {$ifdef DEBUG_NODE_XML}
  493. procedure TRealConstNode.XMLPrintNodeData(var T: Text);
  494. begin
  495. inherited XMLPrintNodeData(T);
  496. WriteLn(T, printnodeindention, '<value>', value_real, '</value>');
  497. end;
  498. {$endif DEBUG_NODE_XML}
  499. {*****************************************************************************
  500. TORDCONSTNODE
  501. *****************************************************************************}
  502. constructor tordconstnode.create(const v : tconstexprint;def:tdef;_rangecheck : boolean);
  503. begin
  504. inherited create(ordconstn);
  505. value:=v;
  506. typedef:=def;
  507. rangecheck := _rangecheck;
  508. end;
  509. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  510. begin
  511. inherited ppuload(t,ppufile);
  512. ppufile.getderef(typedefderef);
  513. value:=ppufile.getexprint;
  514. { normally, the value is already compiled, so we don't need
  515. to do once again a range check
  516. }
  517. rangecheck := false;
  518. end;
  519. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  520. begin
  521. inherited ppuwrite(ppufile);
  522. ppufile.putderef(typedefderef);
  523. ppufile.putexprint(value);
  524. end;
  525. procedure tordconstnode.buildderefimpl;
  526. begin
  527. inherited buildderefimpl;
  528. typedefderef.build(typedef);
  529. end;
  530. procedure tordconstnode.derefimpl;
  531. begin
  532. inherited derefimpl;
  533. typedef:=tdef(typedefderef.resolve);
  534. end;
  535. function tordconstnode.dogetcopy : tnode;
  536. var
  537. n : tordconstnode;
  538. begin
  539. n:=tordconstnode(inherited dogetcopy);
  540. n.value:=value;
  541. n.typedef := typedef;
  542. dogetcopy:=n;
  543. end;
  544. function tordconstnode.pass_typecheck:tnode;
  545. begin
  546. result:=nil;
  547. resultdef:=typedef;
  548. { only do range checking when explicitly asked for it
  549. and if the type can be range checked, see tests/tbs/tb0539.pp }
  550. if (resultdef.typ in [orddef,enumdef]) then
  551. adaptrange(resultdef,value,nf_internal in flags,not rangecheck,rangecheck)
  552. end;
  553. function tordconstnode.pass_1 : tnode;
  554. begin
  555. result:=nil;
  556. expectloc:=LOC_CONSTANT;
  557. end;
  558. function tordconstnode.docompare(p: tnode): boolean;
  559. begin
  560. docompare :=
  561. inherited docompare(p) and
  562. (value = tordconstnode(p).value) and
  563. equal_defs(typedef,tordconstnode(p).typedef);
  564. end;
  565. procedure tordconstnode.printnodedata(var t: text);
  566. begin
  567. inherited printnodedata(t);
  568. writeln(t,printnodeindention,'value = ',tostr(value));
  569. end;
  570. function tordconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  571. begin
  572. tcb.emit_ord_const(value,resultdef);
  573. result:=resultdef.size;
  574. end;
  575. {$ifdef DEBUG_NODE_XML}
  576. procedure TOrdConstNode.XMLPrintNodeInfo(var T: Text);
  577. begin
  578. inherited XMLPrintNodeInfo(T);
  579. Write(T, ' rangecheck="', rangecheck, '"');
  580. end;
  581. procedure TOrdConstNode.XMLPrintNodeData(var T: Text);
  582. begin
  583. inherited XMLPrintNodeData(T);
  584. WriteLn(T, printnodeindention, '<value>', tostr(value), '</value>');
  585. end;
  586. {$endif DEBUG_NODE_XML}
  587. {*****************************************************************************
  588. TPOINTERCONSTNODE
  589. *****************************************************************************}
  590. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  591. begin
  592. inherited create(pointerconstn);
  593. value:=v;
  594. typedef:=def;
  595. end;
  596. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  597. begin
  598. inherited ppuload(t,ppufile);
  599. ppufile.getderef(typedefderef);
  600. value:=ppufile.getptruint;
  601. end;
  602. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  603. begin
  604. inherited ppuwrite(ppufile);
  605. ppufile.putderef(typedefderef);
  606. ppufile.putptruint(value);
  607. end;
  608. procedure tpointerconstnode.buildderefimpl;
  609. begin
  610. inherited buildderefimpl;
  611. typedefderef.build(typedef);
  612. end;
  613. procedure tpointerconstnode.derefimpl;
  614. begin
  615. inherited derefimpl;
  616. typedef:=tdef(typedefderef.resolve);
  617. end;
  618. function tpointerconstnode.dogetcopy : tnode;
  619. var
  620. n : tpointerconstnode;
  621. begin
  622. n:=tpointerconstnode(inherited dogetcopy);
  623. n.value:=value;
  624. n.typedef := typedef;
  625. dogetcopy:=n;
  626. end;
  627. function tpointerconstnode.pass_typecheck:tnode;
  628. begin
  629. result:=nil;
  630. resultdef:=typedef;
  631. end;
  632. function tpointerconstnode.pass_1 : tnode;
  633. begin
  634. result:=nil;
  635. expectloc:=LOC_CONSTANT;
  636. end;
  637. function tpointerconstnode.docompare(p: tnode): boolean;
  638. begin
  639. docompare :=
  640. inherited docompare(p) and
  641. (value = tpointerconstnode(p).value);
  642. end;
  643. procedure tpointerconstnode.printnodedata(var t : text);
  644. begin
  645. inherited printnodedata(t);
  646. writeln(t,printnodeindention,'value = $',hexstr(PUInt(value),sizeof(PUInt)*2));
  647. end;
  648. function tpointerconstnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  649. begin
  650. if tpointerdef(resultdef).compatible_with_pointerdef_size(tpointerdef(voidpointertype)) then
  651. tcb.emit_tai(tai_const.Create_int_dataptr(value),voidpointertype)
  652. else
  653. tcb.emit_tai(tai_const.Create_int_codeptr(value),voidcodepointertype);
  654. result:=resultdef.size;
  655. end;
  656. {$ifdef DEBUG_NODE_XML}
  657. procedure TPointerConstNode.XMLPrintNodeData(var T: Text);
  658. begin
  659. inherited XMLPrintNodeData(T);
  660. WriteLn(T, PrintNodeIndention, '<value>$', hexstr(PUInt(value),sizeof(PUInt)*2), '</value>');
  661. end;
  662. {$endif DEBUG_NODE_XML}
  663. {*****************************************************************************
  664. TSTRINGCONSTNODE
  665. *****************************************************************************}
  666. constructor tstringconstnode.createstr(const s : string);
  667. var
  668. l : longint;
  669. begin
  670. inherited create(stringconstn);
  671. l:=length(s);
  672. len:=l;
  673. { stringdup write even past a #0 }
  674. getmem(value_str,l+1);
  675. move(s[1],value_str^,l);
  676. value_str[l]:=#0;
  677. lab_str:=nil;
  678. cst_type:=cst_conststring;
  679. end;
  680. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  681. begin
  682. inherited create(stringconstn);
  683. len:=getlengthwidestring(w);
  684. initwidestring(pcompilerwidestring(value_str));
  685. copywidestring(w,pcompilerwidestring(value_str));
  686. lab_str:=nil;
  687. cst_type:=cst_unicodestring;
  688. end;
  689. constructor tstringconstnode.createpchar(s: pchar; l: longint; def: tdef);
  690. begin
  691. inherited create(stringconstn);
  692. len:=l;
  693. value_str:=s;
  694. if assigned(def) and
  695. is_ansistring(def) then
  696. begin
  697. cst_type:=cst_ansistring;
  698. astringdef:=def;
  699. end
  700. else
  701. cst_type:=cst_conststring;
  702. lab_str:=nil;
  703. end;
  704. destructor tstringconstnode.destroy;
  705. begin
  706. if cst_type in [cst_widestring,cst_unicodestring] then
  707. donewidestring(pcompilerwidestring(value_str))
  708. else
  709. ansistringdispose(value_str,len);
  710. inherited destroy;
  711. end;
  712. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  713. var
  714. pw : pcompilerwidestring;
  715. i : longint;
  716. begin
  717. inherited ppuload(t,ppufile);
  718. cst_type:=tconststringtype(ppufile.getbyte);
  719. len:=ppufile.getlongint;
  720. if cst_type in [cst_widestring,cst_unicodestring] then
  721. begin
  722. initwidestring(pw);
  723. setlengthwidestring(pw,len);
  724. { don't use getdata, because the compilerwidechars may have to
  725. be byteswapped
  726. }
  727. {$if sizeof(tcompilerwidechar) = 2}
  728. for i:=0 to pw^.len-1 do
  729. pw^.data[i]:=ppufile.getword;
  730. {$elseif sizeof(tcompilerwidechar) = 4}
  731. for i:=0 to pw^.len-1 do
  732. pw^.data[i]:=cardinal(ppufile.getlongint);
  733. {$else}
  734. {$error Unsupported tcompilerwidechar size}
  735. {$endif}
  736. pcompilerwidestring(value_str):=pw
  737. end
  738. else
  739. begin
  740. getmem(value_str,len+1);
  741. ppufile.getdata(value_str^,len);
  742. value_str[len]:=#0;
  743. end;
  744. lab_str:=tasmlabel(ppufile.getasmsymbol);
  745. if cst_type=cst_ansistring then
  746. ppufile.getderef(astringdefderef);
  747. end;
  748. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  749. begin
  750. inherited ppuwrite(ppufile);
  751. ppufile.putbyte(byte(cst_type));
  752. ppufile.putlongint(len);
  753. if cst_type in [cst_widestring,cst_unicodestring] then
  754. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  755. else
  756. ppufile.putdata(value_str^,len);
  757. ppufile.putasmsymbol(lab_str);
  758. if cst_type=cst_ansistring then
  759. ppufile.putderef(astringdefderef);
  760. end;
  761. procedure tstringconstnode.buildderefimpl;
  762. begin
  763. inherited buildderefimpl;
  764. if cst_type=cst_ansistring then
  765. astringdefderef.build(astringdef);
  766. end;
  767. procedure tstringconstnode.derefimpl;
  768. begin
  769. inherited derefimpl;
  770. if cst_type=cst_ansistring then
  771. astringdef:=tdef(astringdefderef.resolve);
  772. end;
  773. function tstringconstnode.dogetcopy : tnode;
  774. var
  775. n : tstringconstnode;
  776. begin
  777. n:=tstringconstnode(inherited dogetcopy);
  778. n.cst_type:=cst_type;
  779. n.len:=len;
  780. n.lab_str:=lab_str;
  781. if cst_type in [cst_widestring,cst_unicodestring] then
  782. begin
  783. initwidestring(pcompilerwidestring(n.value_str));
  784. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  785. end
  786. else
  787. n.value_str:=getpcharcopy;
  788. n.astringdef:=astringdef;
  789. dogetcopy:=n;
  790. end;
  791. function tstringconstnode.pass_typecheck:tnode;
  792. var
  793. l : aint;
  794. begin
  795. result:=nil;
  796. case cst_type of
  797. cst_conststring :
  798. begin
  799. { handle and store as array[0..len-1] of char }
  800. if len>0 then
  801. l:=len-1
  802. else
  803. l:=0;
  804. resultdef:=carraydef.create(0,l,s32inttype);
  805. tarraydef(resultdef).elementdef:=cansichartype;
  806. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  807. end;
  808. cst_shortstring :
  809. resultdef:=cshortstringtype;
  810. cst_ansistring :
  811. if not assigned(astringdef) then
  812. resultdef:=getansistringdef
  813. else
  814. resultdef:=astringdef;
  815. cst_unicodestring :
  816. resultdef:=cunicodestringtype;
  817. cst_widestring :
  818. resultdef:=cwidestringtype;
  819. cst_longstring :
  820. resultdef:=clongstringtype;
  821. end;
  822. end;
  823. function tstringconstnode.pass_1 : tnode;
  824. begin
  825. result:=nil;
  826. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  827. begin
  828. if len=0 then
  829. expectloc:=LOC_CONSTANT
  830. else
  831. expectloc:=LOC_REGISTER
  832. end
  833. else
  834. expectloc:=LOC_CREFERENCE;
  835. end;
  836. function tstringconstnode.getpcharcopy : pchar;
  837. var
  838. pc : pchar;
  839. begin
  840. pc:=nil;
  841. getmem(pc,len+1);
  842. if pc=nil then
  843. Message(general_f_no_memory_left);
  844. move(value_str^,pc^,len+1);
  845. getpcharcopy:=pc;
  846. end;
  847. function tstringconstnode.docompare(p: tnode): boolean;
  848. begin
  849. docompare :=
  850. inherited docompare(p) and
  851. (len = tstringconstnode(p).len) and
  852. (lab_str = tstringconstnode(p).lab_str) and
  853. { This is enough as soon as labels are allocated, otherwise }
  854. { fall back to content compare. }
  855. (assigned(lab_str) or
  856. (cst_type = tstringconstnode(p).cst_type) and
  857. (fullcompare(tstringconstnode(p)) = 0))
  858. ;
  859. end;
  860. procedure tstringconstnode.changestringtype(def:tdef);
  861. const
  862. st2cst : array[tstringtype] of tconststringtype = (
  863. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  864. var
  865. pw : pcompilerwidestring;
  866. pc : pchar;
  867. cp1 : tstringencoding;
  868. cp2 : tstringencoding;
  869. l,l2 : longint;
  870. begin
  871. if def.typ<>stringdef then
  872. internalerror(200510011);
  873. { convert ascii 2 unicode }
  874. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  875. not(cst_type in [cst_widestring,cst_unicodestring]) then
  876. begin
  877. initwidestring(pw);
  878. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  879. ansistringdispose(value_str,len);
  880. pcompilerwidestring(value_str):=pw;
  881. end
  882. else
  883. { convert unicode 2 ascii }
  884. if (cst_type in [cst_widestring,cst_unicodestring]) and
  885. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  886. begin
  887. cp1:=tstringdef(def).encoding;
  888. if (cp1=globals.CP_NONE) or (cp1=0) then
  889. cp1:=current_settings.sourcecodepage;
  890. if (cp1=CP_UTF8) then
  891. begin
  892. pw:=pcompilerwidestring(value_str);
  893. l2:=len;
  894. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  895. getmem(pc,l);
  896. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  897. len:=l-1;
  898. donewidestring(pw);
  899. value_str:=pc;
  900. end
  901. else
  902. begin
  903. pw:=pcompilerwidestring(value_str);
  904. getmem(pc,getlengthwidestring(pw)+1);
  905. unicode2ascii(pw,pc,cp1);
  906. donewidestring(pw);
  907. value_str:=pc;
  908. end;
  909. end
  910. else
  911. if (tstringdef(def).stringtype = st_ansistring) and
  912. not(cst_type in [cst_widestring,cst_unicodestring]) then
  913. begin
  914. cp1:=tstringdef(def).encoding;
  915. if cp1=0 then
  916. cp1:=current_settings.sourcecodepage;
  917. if (cst_type = cst_ansistring) then
  918. begin
  919. cp2:=tstringdef(resultdef).encoding;
  920. if cp2=0 then
  921. cp2:=current_settings.sourcecodepage;
  922. end
  923. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  924. cp2:=current_settings.sourcecodepage
  925. else
  926. internalerror(2013112916);
  927. { don't change string if codepages are equal or string length is 0 }
  928. if (cp1<>cp2) and (len>0) then
  929. begin
  930. if cpavailable(cp1) and cpavailable(cp2) then
  931. changecodepage(value_str,len,cp2,value_str,cp1)
  932. else if (cp1 <> globals.CP_NONE) and (cp2 <> globals.CP_NONE) then
  933. begin
  934. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  935. if (cp2=CP_UTF8) then
  936. begin
  937. if not cpavailable(cp1) then
  938. begin
  939. Message1(option_code_page_not_available,IntToStr(cp1));
  940. exit;
  941. end;
  942. initwidestring(pw);
  943. setlengthwidestring(pw,len);
  944. { returns room for terminating 0 }
  945. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  946. if (l<>getlengthwidestring(pw)) then
  947. begin
  948. setlengthwidestring(pw,l);
  949. ReAllocMem(value_str,l);
  950. end;
  951. unicode2ascii(pw,value_str,cp1);
  952. len:=l-1;
  953. donewidestring(pw);
  954. end
  955. else
  956. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  957. if (cp1=CP_UTF8) then
  958. begin
  959. if not cpavailable(cp2) then
  960. begin
  961. Message1(option_code_page_not_available,IntToStr(cp2));
  962. exit;
  963. end;
  964. initwidestring(pw);
  965. setlengthwidestring(pw,len);
  966. ascii2unicode(value_str,len,cp2,pw);
  967. { returns room for terminating 0 }
  968. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  969. if l<>len then
  970. ReAllocMem(value_str,l);
  971. len:=l-1;
  972. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  973. donewidestring(pw);
  974. end
  975. else
  976. begin
  977. { output error message that encoding is not available for the compiler }
  978. if not cpavailable(cp1) then
  979. Message1(option_code_page_not_available,IntToStr(cp1));
  980. if not cpavailable(cp2) then
  981. Message1(option_code_page_not_available,IntToStr(cp2));
  982. end;
  983. end;
  984. end;
  985. end;
  986. cst_type:=st2cst[tstringdef(def).stringtype];
  987. resultdef:=def;
  988. end;
  989. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  990. begin
  991. if cst_type<>p.cst_type then
  992. InternalError(2009121701);
  993. if cst_type in [cst_widestring,cst_unicodestring] then
  994. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  995. else
  996. result:=compareansistrings(value_str,p.value_str,len,p.len);
  997. end;
  998. function tstringconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  999. var
  1000. ss : shortstring;
  1001. labofs : tasmlabofs;
  1002. winlikewidestring : boolean;
  1003. begin
  1004. case tstringdef(resultdef).stringtype of
  1005. st_shortstring:
  1006. begin
  1007. setlength(ss,len);
  1008. move(value_str^,ss[1],len);
  1009. tcb.emit_shortstring_const(ss);
  1010. result:=len+1;
  1011. end;
  1012. st_longstring:
  1013. internalerror(2019070801);
  1014. st_ansistring:
  1015. begin
  1016. labofs:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  1017. tcb.emit_string_offset(labofs,len,tstringdef(resultdef).stringtype,false,charpointertype);
  1018. result:=voidpointertype.size;
  1019. end;
  1020. st_widestring,
  1021. st_unicodestring:
  1022. begin
  1023. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  1024. labofs:=tcb.emit_unicodestring_const(current_asmdata.asmlists[al_typedconsts],value_str,tstringdef(resultdef).encoding,winlikewidestring);
  1025. tcb.emit_string_offset(labofs,len,tstringdef(resultdef).stringtype,false,widecharpointertype);
  1026. result:=voidpointertype.size;
  1027. end;
  1028. end;
  1029. end;
  1030. class function tstringconstnode.emptydynstrnil: boolean;
  1031. begin
  1032. result:=true;
  1033. end;
  1034. {$ifdef DEBUG_NODE_XML}
  1035. procedure TStringConstNode.XMLPrintNodeData(var T: Text);
  1036. var
  1037. OutputStr: ansistring;
  1038. begin
  1039. inherited XMLPrintNodeData(T);
  1040. Write(T, printnodeindention, '<stringtype>');
  1041. case cst_type of
  1042. cst_conststring:
  1043. Write(T, 'conststring');
  1044. cst_shortstring:
  1045. Write(T, 'shortstring');
  1046. cst_longstring:
  1047. Write(T, 'longstring');
  1048. cst_ansistring:
  1049. Write(T, 'ansistring');
  1050. cst_widestring:
  1051. Write(T, 'widestring');
  1052. cst_unicodestring:
  1053. Write(T, 'unicodestring');
  1054. end;
  1055. WriteLn(T, '</stringtype>');
  1056. WriteLn(T, printnodeindention, '<length>', len, '</length>');
  1057. if len = 0 then
  1058. begin
  1059. WriteLn(T, printnodeindention, '<value />');
  1060. Exit;
  1061. end;
  1062. case cst_type of
  1063. cst_widestring, cst_unicodestring:
  1064. begin
  1065. { value_str is of type PCompilerWideString }
  1066. SetLength(OutputStr, len);
  1067. UnicodeToUtf8(PChar(OutputStr), PUnicodeChar(PCompilerWideString(value_str)^.data), len + 1); { +1 for the null terminator }
  1068. end;
  1069. else
  1070. OutputStr := ansistring(value_str);
  1071. SetLength(OutputStr, len);
  1072. end;
  1073. WriteLn(T, printnodeindention, '<value>', SanitiseXMLString(OutputStr), '</value>');
  1074. end;
  1075. {$endif DEBUG_NODE_XML}
  1076. {*****************************************************************************
  1077. TSETCONSTNODE
  1078. *****************************************************************************}
  1079. constructor tsetconstnode.create(s : pconstset;def:tdef);
  1080. begin
  1081. inherited create(setconstn,nil);
  1082. typedef:=def;
  1083. if assigned(s) then
  1084. begin
  1085. new(value_set);
  1086. value_set^:=s^;
  1087. end
  1088. else
  1089. value_set:=nil;
  1090. end;
  1091. destructor tsetconstnode.destroy;
  1092. begin
  1093. if assigned(value_set) then
  1094. dispose(value_set);
  1095. inherited destroy;
  1096. end;
  1097. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1098. begin
  1099. inherited ppuload(t,ppufile);
  1100. ppufile.getderef(typedefderef);
  1101. new(value_set);
  1102. ppufile.getset(tppuset32(value_set^));
  1103. end;
  1104. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1105. begin
  1106. inherited ppuwrite(ppufile);
  1107. ppufile.putderef(typedefderef);
  1108. ppufile.putset(tppuset32(value_set^));
  1109. end;
  1110. procedure tsetconstnode.buildderefimpl;
  1111. begin
  1112. inherited buildderefimpl;
  1113. typedefderef.build(typedef);
  1114. end;
  1115. procedure tsetconstnode.derefimpl;
  1116. begin
  1117. inherited derefimpl;
  1118. typedef:=tdef(typedefderef.resolve);
  1119. end;
  1120. type
  1121. setbytes = array[0..31] of byte;
  1122. Psetbytes = ^setbytes;
  1123. procedure tsetconstnode.adjustforsetbase;
  1124. var
  1125. i, diff: longint;
  1126. begin
  1127. { Internally, the compiler stores all sets with setbase 0, so we have }
  1128. { to convert the set to its actual format in case setbase<>0 when }
  1129. { writing it out }
  1130. if (tsetdef(resultdef).setbase<>0) then
  1131. begin
  1132. if (tsetdef(resultdef).setbase and 7)<>0 then
  1133. internalerror(2007091501);
  1134. diff:=tsetdef(resultdef).setbase div 8;
  1135. { This is endian-neutral in the new set format: in both cases, }
  1136. { the first byte contains the first elements of the set. }
  1137. { Since the compiler/base rtl cannot contain packed sets before }
  1138. { they work for big endian, it's no problem that the code below }
  1139. { is wrong for the old big endian set format (setbase cannot be }
  1140. { <>0 with non-packed sets). }
  1141. for i:=0 to tsetdef(resultdef).size-1 do
  1142. begin
  1143. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1144. Psetbytes(value_set)^[i+diff]:=0;
  1145. end;
  1146. end;
  1147. end;
  1148. function tsetconstnode.dogetcopy : tnode;
  1149. var
  1150. n : tsetconstnode;
  1151. begin
  1152. n:=tsetconstnode(inherited dogetcopy);
  1153. if assigned(value_set) then
  1154. begin
  1155. new(n.value_set);
  1156. n.value_set^:=value_set^
  1157. end
  1158. else
  1159. n.value_set:=nil;
  1160. n.typedef := typedef;
  1161. n.lab_set:=lab_set;
  1162. dogetcopy:=n;
  1163. end;
  1164. function tsetconstnode.pass_typecheck:tnode;
  1165. begin
  1166. result:=nil;
  1167. resultdef:=typedef;
  1168. end;
  1169. function tsetconstnode.pass_1 : tnode;
  1170. begin
  1171. result:=nil;
  1172. if is_smallset(resultdef) then
  1173. expectloc:=LOC_CONSTANT
  1174. else
  1175. expectloc:=LOC_CREFERENCE;
  1176. end;
  1177. function tsetconstnode.docompare(p: tnode): boolean;
  1178. begin
  1179. docompare:=(inherited docompare(p)) and
  1180. (value_set^=Tsetconstnode(p).value_set^);
  1181. end;
  1182. function tsetconstnode.elements : AInt;
  1183. var
  1184. i : longint;
  1185. begin
  1186. result:=0;
  1187. if not(assigned(value_set)) then
  1188. exit;
  1189. for i:=0 to tsetdef(resultdef).size-1 do
  1190. result:=result+ PopCnt(Psetbytes(value_set)^[i]);
  1191. end;
  1192. function tsetconstnode.emit_data(tcb:ttai_typedconstbuilder):sizeint;
  1193. type
  1194. setbytes=array[0..31] of byte;
  1195. Psetbytes=^setbytes;
  1196. var
  1197. setval : aint;
  1198. i : sizeint;
  1199. begin
  1200. if is_smallset(resultdef) then
  1201. begin
  1202. if (source_info.endian=target_info.endian) then
  1203. begin
  1204. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  1205. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  1206. { cross-endian code below }
  1207. { Extra aint type cast to avoid range errors }
  1208. setval:=aint(pCardinal(value_set)^)
  1209. end
  1210. else
  1211. begin
  1212. setval:=aint(swapendian(Pcardinal(value_set)^));
  1213. setval:=aint(
  1214. reverse_byte (setval and $ff) or
  1215. (reverse_byte((setval shr 8) and $ff) shl 8) or
  1216. (reverse_byte((setval shr 16) and $ff) shl 16) or
  1217. (reverse_byte((setval shr 24) and $ff) shl 24)
  1218. );
  1219. end;
  1220. if (target_info.endian=endian_big) then
  1221. setval:=setval shr (32-resultdef.size*8);
  1222. case resultdef.size of
  1223. 1:
  1224. tcb.emit_ord_const(byte(setval),u8inttype);
  1225. 2:
  1226. tcb.emit_ord_const(word(setval),u16inttype);
  1227. 4:
  1228. tcb.emit_ord_const(longword(setval),u32inttype);
  1229. 8:
  1230. tcb.emit_ord_const(qword(setval),u64inttype);
  1231. else
  1232. internalerror(2019070802);
  1233. end;
  1234. end
  1235. else
  1236. begin
  1237. if (source_info.endian=target_info.endian) then
  1238. for i:=0 to resultdef.size-1 do
  1239. tcb.emit_tai(tai_const.create_8bit(Psetbytes(value_set)^[i]),u8inttype)
  1240. else
  1241. for i:=0 to resultdef.size-1 do
  1242. tcb.emit_tai(tai_const.create_8bit(reverse_byte(Psetbytes(value_set)^[i])),u8inttype);
  1243. end;
  1244. result:=resultdef.size;
  1245. end;
  1246. {*****************************************************************************
  1247. TNILNODE
  1248. *****************************************************************************}
  1249. constructor tnilnode.create;
  1250. begin
  1251. inherited create(niln);
  1252. end;
  1253. function tnilnode.pass_typecheck:tnode;
  1254. begin
  1255. result:=nil;
  1256. resultdef:=voidpointertype;
  1257. end;
  1258. function tnilnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  1259. begin
  1260. if tpointerdef(resultdef).compatible_with_pointerdef_size(tpointerdef(voidpointertype)) then
  1261. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype)
  1262. else
  1263. tcb.emit_tai(tai_const.Create_nil_codeptr,voidcodepointertype);
  1264. result:=resultdef.size;
  1265. end;
  1266. function tnilnode.pass_1 : tnode;
  1267. begin
  1268. result:=nil;
  1269. expectloc:=LOC_CONSTANT;
  1270. end;
  1271. {*****************************************************************************
  1272. TGUIDCONSTNODE
  1273. *****************************************************************************}
  1274. constructor tguidconstnode.create(const g:tguid);
  1275. begin
  1276. inherited create(guidconstn);
  1277. value:=g;
  1278. end;
  1279. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1280. begin
  1281. inherited ppuload(t,ppufile);
  1282. ppufile.getguid(value);
  1283. end;
  1284. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1285. begin
  1286. inherited ppuwrite(ppufile);
  1287. ppufile.putguid(value);
  1288. end;
  1289. function tguidconstnode.dogetcopy : tnode;
  1290. var
  1291. n : tguidconstnode;
  1292. begin
  1293. n:=tguidconstnode(inherited dogetcopy);
  1294. n.value:=value;
  1295. n.lab_set:=lab_set;
  1296. dogetcopy:=n;
  1297. end;
  1298. function tguidconstnode.pass_typecheck:tnode;
  1299. begin
  1300. result:=nil;
  1301. resultdef:=rec_tguid;
  1302. end;
  1303. function tguidconstnode.pass_1 : tnode;
  1304. begin
  1305. result:=nil;
  1306. expectloc:=LOC_CREFERENCE;
  1307. end;
  1308. function tguidconstnode.docompare(p: tnode): boolean;
  1309. begin
  1310. docompare :=
  1311. inherited docompare(p) and
  1312. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1313. end;
  1314. function tguidconstnode.emit_data(tcb: ttai_typedconstbuilder): sizeint;
  1315. begin
  1316. tcb.emit_guid_const(value);
  1317. result:=resultdef.size;
  1318. end;
  1319. end.