ncon.pas 50 KB

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