ncon.pas 51 KB

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