ncon.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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,cpuinfo,globals,
  24. symconst,symtype,symdef,symsym;
  25. type
  26. trealconstnode = class(tnode)
  27. typedef : tdef;
  28. typedefderef : tderef;
  29. value_real : bestreal;
  30. value_currency : currency;
  31. lab_real : tasmlabel;
  32. constructor create(v : bestreal;def:tdef);virtual;
  33. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  34. procedure ppuwrite(ppufile:tcompilerppufile);override;
  35. procedure buildderefimpl;override;
  36. procedure derefimpl;override;
  37. function dogetcopy : tnode;override;
  38. function pass_1 : tnode;override;
  39. function pass_typecheck:tnode;override;
  40. function docompare(p: tnode) : boolean; override;
  41. procedure printnodedata(var t:text);override;
  42. end;
  43. trealconstnodeclass = class of trealconstnode;
  44. tordconstnode = class(tnode)
  45. typedef : tdef;
  46. typedefderef : tderef;
  47. value : TConstExprInt;
  48. rangecheck : boolean;
  49. { create an ordinal constant node of the specified type and value.
  50. _rangecheck determines if the value of the ordinal should be checked
  51. against the ranges of the type definition.
  52. }
  53. constructor create(const v : tconstexprint;def:tdef; _rangecheck : boolean);virtual;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. procedure buildderefimpl;override;
  57. procedure derefimpl;override;
  58. function dogetcopy : tnode;override;
  59. function pass_1 : tnode;override;
  60. function pass_typecheck:tnode;override;
  61. function docompare(p: tnode) : boolean; override;
  62. procedure printnodedata(var t:text);override;
  63. end;
  64. tordconstnodeclass = class of tordconstnode;
  65. tpointerconstnode = class(tnode)
  66. typedef : tdef;
  67. typedefderef : tderef;
  68. value : TConstPtrUInt;
  69. constructor create(v : TConstPtrUInt;def:tdef);virtual;
  70. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  71. procedure ppuwrite(ppufile:tcompilerppufile);override;
  72. procedure buildderefimpl;override;
  73. procedure derefimpl;override;
  74. function dogetcopy : tnode;override;
  75. function pass_1 : tnode;override;
  76. function pass_typecheck:tnode;override;
  77. function docompare(p: tnode) : boolean; override;
  78. procedure printnodedata(var t : text); override;
  79. end;
  80. tpointerconstnodeclass = class of tpointerconstnode;
  81. tconststringtype = (
  82. cst_conststring,
  83. cst_shortstring,
  84. cst_longstring,
  85. cst_ansistring,
  86. cst_widestring,
  87. cst_unicodestring
  88. );
  89. tstringconstnode = class(tnode)
  90. value_str : pchar;
  91. len : longint;
  92. lab_str : tasmlabel;
  93. astringdef : tdef;
  94. astringdefderef : tderef;
  95. cst_type : tconststringtype;
  96. constructor createstr(const s : string);virtual;
  97. constructor createpchar(s: pchar; l: longint; def: tdef);virtual;
  98. constructor createunistr(w : pcompilerwidestring);virtual;
  99. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. procedure buildderefimpl;override;
  102. procedure derefimpl;override;
  103. destructor destroy;override;
  104. function dogetcopy : tnode;override;
  105. function pass_1 : tnode;override;
  106. function pass_typecheck:tnode;override;
  107. function getpcharcopy : pchar;
  108. function docompare(p: tnode) : boolean; override;
  109. procedure changestringtype(def:tdef);
  110. function fullcompare(p: tstringconstnode): longint;
  111. { returns whether this platform uses the nil pointer to represent
  112. empty dynamic strings }
  113. class function emptydynstrnil: boolean; virtual;
  114. end;
  115. tstringconstnodeclass = class of tstringconstnode;
  116. tsetconstnode = class(tunarynode)
  117. typedef : tdef;
  118. typedefderef : tderef;
  119. value_set : pconstset;
  120. lab_set : tasmsymbol;
  121. constructor create(s : pconstset;def:tdef);virtual;
  122. destructor destroy;override;
  123. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  124. procedure ppuwrite(ppufile:tcompilerppufile);override;
  125. procedure buildderefimpl;override;
  126. procedure derefimpl;override;
  127. procedure adjustforsetbase;
  128. function dogetcopy : tnode;override;
  129. function pass_1 : tnode;override;
  130. function pass_typecheck:tnode;override;
  131. function docompare(p: tnode) : boolean; override;
  132. function elements : AInt;
  133. end;
  134. tsetconstnodeclass = class of tsetconstnode;
  135. tnilnode = class(tnode)
  136. constructor create;virtual;
  137. function pass_1 : tnode;override;
  138. function pass_typecheck:tnode;override;
  139. end;
  140. tnilnodeclass = class of tnilnode;
  141. tguidconstnode = class(tnode)
  142. value : tguid;
  143. lab_set : tasmsymbol;
  144. constructor create(const g:tguid);virtual;
  145. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  146. procedure ppuwrite(ppufile:tcompilerppufile);override;
  147. function dogetcopy : tnode;override;
  148. function pass_1 : tnode;override;
  149. function pass_typecheck:tnode;override;
  150. function docompare(p: tnode) : boolean; override;
  151. end;
  152. tguidconstnodeclass = class of tguidconstnode;
  153. var
  154. crealconstnode : trealconstnodeclass = trealconstnode;
  155. cordconstnode : tordconstnodeclass = tordconstnode;
  156. cpointerconstnode : tpointerconstnodeclass = tpointerconstnode;
  157. cstringconstnode : tstringconstnodeclass = tstringconstnode;
  158. csetconstnode : tsetconstnodeclass = tsetconstnode;
  159. cguidconstnode : tguidconstnodeclass = tguidconstnode;
  160. cnilnode : tnilnodeclass=tnilnode;
  161. function genintconstnode(const v : TConstExprInt) : tordconstnode;
  162. function genenumnode(v : tenumsym) : tordconstnode;
  163. { some helper routines }
  164. function get_ordinal_value(p : tnode) : TConstExprInt;
  165. function get_string_value(p : tnode; def: tstringdef) : tstringconstnode;
  166. function is_constresourcestringnode(p : tnode) : boolean;
  167. function is_emptyset(p : tnode):boolean;
  168. function genconstsymtree(p : tconstsym) : tnode;
  169. function getbooleanvalue(p : tnode) : boolean;
  170. implementation
  171. uses
  172. cutils,
  173. verbose,systems,sysutils,
  174. defcmp,defutil,procinfo,
  175. cgbase,
  176. nld;
  177. function genintconstnode(const v : TConstExprInt) : tordconstnode;
  178. var
  179. htype : tdef;
  180. begin
  181. int_to_type(v,htype);
  182. genintconstnode:=cordconstnode.create(v,htype,true);
  183. end;
  184. function genenumnode(v : tenumsym) : tordconstnode;
  185. var
  186. htype : tdef;
  187. begin
  188. htype:=v.definition;
  189. genenumnode:=cordconstnode.create(int64(v.value),htype,true);
  190. end;
  191. function get_ordinal_value(p : tnode) : TConstExprInt;
  192. begin
  193. get_ordinal_value:=0;
  194. if is_constnode(p) then
  195. begin
  196. if p.nodetype=ordconstn then
  197. get_ordinal_value:=tordconstnode(p).value
  198. else
  199. Message(type_e_ordinal_expr_expected);
  200. end
  201. else
  202. Message(type_e_constant_expr_expected);
  203. end;
  204. function get_string_value(p: tnode; def: tstringdef): tstringconstnode;
  205. var
  206. stringVal: string;
  207. pWideStringVal: pcompilerwidestring;
  208. begin
  209. stringVal:='';
  210. if is_constcharnode(p) then
  211. begin
  212. SetLength(stringVal,1);
  213. stringVal[1]:=char(tordconstnode(p).value.uvalue);
  214. result:=cstringconstnode.createstr(stringVal);
  215. end
  216. else if is_constwidecharnode(p) then
  217. begin
  218. initwidestring(pWideStringVal);
  219. concatwidestringchar(pWideStringVal, tcompilerwidechar(tordconstnode(p).value.uvalue));
  220. result:=cstringconstnode.createunistr(pWideStringVal);
  221. end
  222. else if p.nodetype=stringconstn then
  223. result:=tstringconstnode(p.getcopy)
  224. else
  225. begin
  226. Message(type_e_string_expr_expected);
  227. stringVal:='';
  228. result:=cstringconstnode.createstr(stringVal);
  229. end;
  230. result.changestringtype(def);
  231. end;
  232. function is_constresourcestringnode(p : tnode) : boolean;
  233. begin
  234. is_constresourcestringnode:=(p.nodetype=loadn) and
  235. (tloadnode(p).symtableentry.typ=constsym) and
  236. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  237. end;
  238. function is_emptyset(p : tnode):boolean;
  239. begin
  240. is_emptyset:=(p.nodetype=setconstn) and
  241. (Tsetconstnode(p).value_set^=[]);
  242. end;
  243. function genconstsymtree(p : tconstsym) : tnode;
  244. var
  245. p1 : tnode;
  246. len : longint;
  247. pc : pchar;
  248. begin
  249. p1:=nil;
  250. case p.consttyp of
  251. constord :
  252. begin
  253. if p.constdef=nil then
  254. internalerror(200403232);
  255. p1:=cordconstnode.create(p.value.valueord,p.constdef,true);
  256. end;
  257. conststring :
  258. begin
  259. len:=p.value.len;
  260. if not(cs_refcountedstrings in current_settings.localswitches) and (len>255) then
  261. begin
  262. message(parser_e_string_const_too_long);
  263. len:=255;
  264. end;
  265. getmem(pc,len+1);
  266. move(pchar(p.value.valueptr)^,pc^,len);
  267. pc[len]:=#0;
  268. p1:=cstringconstnode.createpchar(pc,len,p.constdef);
  269. end;
  270. constwstring :
  271. p1:=cstringconstnode.createunistr(pcompilerwidestring(p.value.valueptr));
  272. constreal :
  273. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,p.constdef);
  274. constset :
  275. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.constdef);
  276. constpointer :
  277. p1:=cpointerconstnode.create(p.value.valueordptr,p.constdef);
  278. constnil :
  279. p1:=cnilnode.create;
  280. constguid :
  281. p1:=cguidconstnode.create(pguid(p.value.valueptr)^);
  282. else
  283. internalerror(200205103);
  284. end;
  285. genconstsymtree:=p1;
  286. end;
  287. function getbooleanvalue(p : tnode) : boolean;
  288. begin
  289. if is_constboolnode(p) then
  290. result:=tordconstnode(p).value<>0
  291. else
  292. internalerror(2013111601);
  293. end;
  294. {*****************************************************************************
  295. TREALCONSTNODE
  296. *****************************************************************************}
  297. { generic code }
  298. { overridden by: }
  299. { i386 }
  300. constructor trealconstnode.create(v : bestreal;def:tdef);
  301. begin
  302. if current_settings.fputype=fpu_none then
  303. internalerror(2008022401);
  304. inherited create(realconstn);
  305. typedef:=def;
  306. case tfloatdef(def).floattype of
  307. s32real:
  308. v:=single(v);
  309. s64real:
  310. v:=double(v);
  311. s80real,
  312. sc80real,
  313. s64comp,
  314. s64currency:
  315. v:=extended(v);
  316. s128real:
  317. internalerror(2013102701);
  318. else
  319. internalerror(2013102702);
  320. end;
  321. value_real:=v;
  322. value_currency:=v;
  323. lab_real:=nil;
  324. end;
  325. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  326. var
  327. i : int64;
  328. begin
  329. inherited ppuload(t,ppufile);
  330. ppufile.getderef(typedefderef);
  331. value_real:=ppufile.getreal;
  332. i:=ppufile.getint64;
  333. value_currency:=PCurrency(@i)^;
  334. lab_real:=tasmlabel(ppufile.getasmsymbol);
  335. end;
  336. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  337. begin
  338. inherited ppuwrite(ppufile);
  339. ppufile.putderef(typedefderef);
  340. ppufile.putreal(value_real);
  341. ppufile.putint64(PInt64(@value_currency)^);
  342. ppufile.putasmsymbol(lab_real);
  343. end;
  344. procedure trealconstnode.buildderefimpl;
  345. begin
  346. inherited buildderefimpl;
  347. typedefderef.build(typedef);
  348. end;
  349. procedure trealconstnode.derefimpl;
  350. begin
  351. inherited derefimpl;
  352. typedef:=tdef(typedefderef.resolve);
  353. end;
  354. function trealconstnode.dogetcopy : tnode;
  355. var
  356. n : trealconstnode;
  357. begin
  358. n:=trealconstnode(inherited dogetcopy);
  359. n.typedef:=typedef;
  360. n.value_real:=value_real;
  361. n.value_currency:=value_currency;
  362. n.lab_real:=lab_real;
  363. dogetcopy:=n;
  364. end;
  365. function trealconstnode.pass_typecheck:tnode;
  366. begin
  367. result:=nil;
  368. resultdef:=typedef;
  369. { range checking? }
  370. if floating_point_range_check_error or
  371. (tfloatdef(resultdef).floattype in [s64comp,s64currency]) then
  372. begin
  373. { use CGMessage so that the resultdef will get set to errordef
  374. by pass1.typecheckpass_internal if a range error was triggered,
  375. which in turn will prevent any potential parent type conversion
  376. node from creating a new realconstnode with this exact same value
  377. and hence trigger the same error again }
  378. case tfloatdef(resultdef).floattype of
  379. s32real :
  380. begin
  381. if ts32real(value_real)=MathInf.Value then
  382. CGMessage(parser_e_range_check_error);
  383. end;
  384. s64real:
  385. begin
  386. if ts64real(value_real)=MathInf.Value then
  387. CGMessage(parser_e_range_check_error);
  388. end;
  389. s80real,
  390. sc80real:
  391. begin
  392. if ts80real(value_real)=MathInf.Value then
  393. CGMessage(parser_e_range_check_error);
  394. end;
  395. s64comp,
  396. s64currency:
  397. begin
  398. if (value_real>9223372036854775807.0) or
  399. (value_real<-9223372036854775808.0) then
  400. CGMessage(parser_e_range_check_error)
  401. end;
  402. s128real:
  403. begin
  404. if ts128real(value_real)=MathInf.Value then
  405. CGMessage(parser_e_range_check_error);
  406. end;
  407. else
  408. internalerror(2016112902);
  409. end;
  410. end;
  411. end;
  412. function trealconstnode.pass_1 : tnode;
  413. begin
  414. result:=nil;
  415. expectloc:=LOC_CREFERENCE;
  416. if (cs_create_pic in current_settings.moduleswitches) then
  417. include(current_procinfo.flags,pi_needs_got);
  418. end;
  419. function trealconstnode.docompare(p: tnode): boolean;
  420. begin
  421. docompare :=
  422. inherited docompare(p) and
  423. { this should be always true }
  424. (trealconstnode(p).typedef.typ=floatdef) and (typedef.typ=floatdef) and
  425. (tfloatdef(typedef).floattype = tfloatdef(trealconstnode(p).typedef).floattype) and
  426. (
  427. (
  428. (tfloatdef(typedef).floattype=s64currency) and
  429. (value_currency=trealconstnode(p).value_currency)
  430. )
  431. or
  432. (
  433. (tfloatdef(typedef).floattype<>s64currency) and
  434. (value_real = trealconstnode(p).value_real) and
  435. { floating point compares for non-numbers give strange results usually }
  436. is_number_float(value_real) and
  437. is_number_float(trealconstnode(p).value_real)
  438. )
  439. );
  440. end;
  441. procedure Trealconstnode.printnodedata(var t:text);
  442. begin
  443. inherited printnodedata(t);
  444. write(t,printnodeindention,'value = ',value_real);
  445. if is_currency(resultdef) then
  446. writeln(', value_currency = ',value_currency)
  447. else
  448. writeln;
  449. end;
  450. {*****************************************************************************
  451. TORDCONSTNODE
  452. *****************************************************************************}
  453. constructor tordconstnode.create(const v : tconstexprint;def:tdef;_rangecheck : boolean);
  454. begin
  455. inherited create(ordconstn);
  456. value:=v;
  457. typedef:=def;
  458. rangecheck := _rangecheck;
  459. end;
  460. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  461. begin
  462. inherited ppuload(t,ppufile);
  463. ppufile.getderef(typedefderef);
  464. value:=ppufile.getexprint;
  465. { normally, the value is already compiled, so we don't need
  466. to do once again a range check
  467. }
  468. rangecheck := false;
  469. end;
  470. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  471. begin
  472. inherited ppuwrite(ppufile);
  473. ppufile.putderef(typedefderef);
  474. ppufile.putexprint(value);
  475. end;
  476. procedure tordconstnode.buildderefimpl;
  477. begin
  478. inherited buildderefimpl;
  479. typedefderef.build(typedef);
  480. end;
  481. procedure tordconstnode.derefimpl;
  482. begin
  483. inherited derefimpl;
  484. typedef:=tdef(typedefderef.resolve);
  485. end;
  486. function tordconstnode.dogetcopy : tnode;
  487. var
  488. n : tordconstnode;
  489. begin
  490. n:=tordconstnode(inherited dogetcopy);
  491. n.value:=value;
  492. n.typedef := typedef;
  493. dogetcopy:=n;
  494. end;
  495. function tordconstnode.pass_typecheck:tnode;
  496. begin
  497. result:=nil;
  498. resultdef:=typedef;
  499. { only do range checking when explicitly asked for it
  500. and if the type can be range checked, see tests/tbs/tb0539.pp }
  501. if (resultdef.typ in [orddef,enumdef]) then
  502. adaptrange(resultdef,value,nf_internal in flags,not rangecheck,rangecheck)
  503. end;
  504. function tordconstnode.pass_1 : tnode;
  505. begin
  506. result:=nil;
  507. expectloc:=LOC_CONSTANT;
  508. end;
  509. function tordconstnode.docompare(p: tnode): boolean;
  510. begin
  511. docompare :=
  512. inherited docompare(p) and
  513. (value = tordconstnode(p).value) and
  514. equal_defs(typedef,tordconstnode(p).typedef);
  515. end;
  516. procedure Tordconstnode.printnodedata(var t:text);
  517. begin
  518. inherited printnodedata(t);
  519. writeln(t,printnodeindention,'value = ',tostr(value));
  520. end;
  521. {*****************************************************************************
  522. TPOINTERCONSTNODE
  523. *****************************************************************************}
  524. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  525. begin
  526. inherited create(pointerconstn);
  527. value:=v;
  528. typedef:=def;
  529. end;
  530. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  531. begin
  532. inherited ppuload(t,ppufile);
  533. ppufile.getderef(typedefderef);
  534. value:=ppufile.getptruint;
  535. end;
  536. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  537. begin
  538. inherited ppuwrite(ppufile);
  539. ppufile.putderef(typedefderef);
  540. ppufile.putptruint(value);
  541. end;
  542. procedure tpointerconstnode.buildderefimpl;
  543. begin
  544. inherited buildderefimpl;
  545. typedefderef.build(typedef);
  546. end;
  547. procedure tpointerconstnode.derefimpl;
  548. begin
  549. inherited derefimpl;
  550. typedef:=tdef(typedefderef.resolve);
  551. end;
  552. function tpointerconstnode.dogetcopy : tnode;
  553. var
  554. n : tpointerconstnode;
  555. begin
  556. n:=tpointerconstnode(inherited dogetcopy);
  557. n.value:=value;
  558. n.typedef := typedef;
  559. dogetcopy:=n;
  560. end;
  561. function tpointerconstnode.pass_typecheck:tnode;
  562. begin
  563. result:=nil;
  564. resultdef:=typedef;
  565. end;
  566. function tpointerconstnode.pass_1 : tnode;
  567. begin
  568. result:=nil;
  569. expectloc:=LOC_CONSTANT;
  570. end;
  571. function tpointerconstnode.docompare(p: tnode): boolean;
  572. begin
  573. docompare :=
  574. inherited docompare(p) and
  575. (value = tpointerconstnode(p).value);
  576. end;
  577. procedure tpointerconstnode.printnodedata(var t : text);
  578. begin
  579. inherited printnodedata(t);
  580. writeln(t,printnodeindention,'value = $',hexstr(PUInt(value),sizeof(PUInt)*2));
  581. end;
  582. {*****************************************************************************
  583. TSTRINGCONSTNODE
  584. *****************************************************************************}
  585. constructor tstringconstnode.createstr(const s : string);
  586. var
  587. l : longint;
  588. begin
  589. inherited create(stringconstn);
  590. l:=length(s);
  591. len:=l;
  592. { stringdup write even past a #0 }
  593. getmem(value_str,l+1);
  594. move(s[1],value_str^,l);
  595. value_str[l]:=#0;
  596. lab_str:=nil;
  597. cst_type:=cst_conststring;
  598. end;
  599. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  600. begin
  601. inherited create(stringconstn);
  602. len:=getlengthwidestring(w);
  603. initwidestring(pcompilerwidestring(value_str));
  604. copywidestring(w,pcompilerwidestring(value_str));
  605. lab_str:=nil;
  606. cst_type:=cst_unicodestring;
  607. end;
  608. constructor tstringconstnode.createpchar(s: pchar; l: longint; def: tdef);
  609. begin
  610. inherited create(stringconstn);
  611. len:=l;
  612. value_str:=s;
  613. if assigned(def) and
  614. is_ansistring(def) then
  615. begin
  616. cst_type:=cst_ansistring;
  617. astringdef:=def;
  618. end
  619. else
  620. cst_type:=cst_conststring;
  621. lab_str:=nil;
  622. end;
  623. destructor tstringconstnode.destroy;
  624. begin
  625. if cst_type in [cst_widestring,cst_unicodestring] then
  626. donewidestring(pcompilerwidestring(value_str))
  627. else
  628. ansistringdispose(value_str,len);
  629. inherited destroy;
  630. end;
  631. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  632. var
  633. pw : pcompilerwidestring;
  634. i : longint;
  635. begin
  636. inherited ppuload(t,ppufile);
  637. cst_type:=tconststringtype(ppufile.getbyte);
  638. len:=ppufile.getlongint;
  639. if cst_type in [cst_widestring,cst_unicodestring] then
  640. begin
  641. initwidestring(pw);
  642. setlengthwidestring(pw,len);
  643. { don't use getdata, because the compilerwidechars may have to
  644. be byteswapped
  645. }
  646. {$if sizeof(tcompilerwidechar) = 2}
  647. for i:=0 to pw^.len-1 do
  648. pw^.data[i]:=ppufile.getword;
  649. {$elseif sizeof(tcompilerwidechar) = 4}
  650. for i:=0 to pw^.len-1 do
  651. pw^.data[i]:=cardinal(ppufile.getlongint);
  652. {$else}
  653. {$error Unsupported tcompilerwidechar size}
  654. {$endif}
  655. pcompilerwidestring(value_str):=pw
  656. end
  657. else
  658. begin
  659. getmem(value_str,len+1);
  660. ppufile.getdata(value_str^,len);
  661. value_str[len]:=#0;
  662. end;
  663. lab_str:=tasmlabel(ppufile.getasmsymbol);
  664. if cst_type=cst_ansistring then
  665. ppufile.getderef(astringdefderef);
  666. end;
  667. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  668. begin
  669. inherited ppuwrite(ppufile);
  670. ppufile.putbyte(byte(cst_type));
  671. ppufile.putlongint(len);
  672. if cst_type in [cst_widestring,cst_unicodestring] then
  673. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  674. else
  675. ppufile.putdata(value_str^,len);
  676. ppufile.putasmsymbol(lab_str);
  677. if cst_type=cst_ansistring then
  678. ppufile.putderef(astringdefderef);
  679. end;
  680. procedure tstringconstnode.buildderefimpl;
  681. begin
  682. inherited buildderefimpl;
  683. if cst_type=cst_ansistring then
  684. astringdefderef.build(astringdef);
  685. end;
  686. procedure tstringconstnode.derefimpl;
  687. begin
  688. inherited derefimpl;
  689. if cst_type=cst_ansistring then
  690. astringdef:=tdef(astringdefderef.resolve);
  691. end;
  692. function tstringconstnode.dogetcopy : tnode;
  693. var
  694. n : tstringconstnode;
  695. begin
  696. n:=tstringconstnode(inherited dogetcopy);
  697. n.cst_type:=cst_type;
  698. n.len:=len;
  699. n.lab_str:=lab_str;
  700. if cst_type in [cst_widestring,cst_unicodestring] then
  701. begin
  702. initwidestring(pcompilerwidestring(n.value_str));
  703. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  704. end
  705. else
  706. n.value_str:=getpcharcopy;
  707. n.astringdef:=astringdef;
  708. dogetcopy:=n;
  709. end;
  710. function tstringconstnode.pass_typecheck:tnode;
  711. var
  712. l : aint;
  713. begin
  714. result:=nil;
  715. case cst_type of
  716. cst_conststring :
  717. begin
  718. { handle and store as array[0..len-1] of char }
  719. if len>0 then
  720. l:=len-1
  721. else
  722. l:=0;
  723. resultdef:=carraydef.create(0,l,s32inttype);
  724. tarraydef(resultdef).elementdef:=cansichartype;
  725. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  726. end;
  727. cst_shortstring :
  728. resultdef:=cshortstringtype;
  729. cst_ansistring :
  730. if not assigned(astringdef) then
  731. resultdef:=getansistringdef
  732. else
  733. resultdef:=astringdef;
  734. cst_unicodestring :
  735. resultdef:=cunicodestringtype;
  736. cst_widestring :
  737. resultdef:=cwidestringtype;
  738. cst_longstring :
  739. resultdef:=clongstringtype;
  740. end;
  741. end;
  742. function tstringconstnode.pass_1 : tnode;
  743. begin
  744. result:=nil;
  745. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  746. begin
  747. if len=0 then
  748. expectloc:=LOC_CONSTANT
  749. else
  750. expectloc:=LOC_REGISTER
  751. end
  752. else
  753. expectloc:=LOC_CREFERENCE;
  754. if (cs_create_pic in current_settings.moduleswitches) and
  755. (expectloc <> LOC_CONSTANT) then
  756. include(current_procinfo.flags,pi_needs_got);
  757. end;
  758. function tstringconstnode.getpcharcopy : pchar;
  759. var
  760. pc : pchar;
  761. begin
  762. pc:=nil;
  763. getmem(pc,len+1);
  764. if pc=nil then
  765. Message(general_f_no_memory_left);
  766. move(value_str^,pc^,len+1);
  767. getpcharcopy:=pc;
  768. end;
  769. function tstringconstnode.docompare(p: tnode): boolean;
  770. begin
  771. docompare :=
  772. inherited docompare(p) and
  773. (len = tstringconstnode(p).len) and
  774. (lab_str = tstringconstnode(p).lab_str) and
  775. { This is enough as soon as labels are allocated, otherwise }
  776. { fall back to content compare. }
  777. (assigned(lab_str) or
  778. (cst_type = tstringconstnode(p).cst_type) and
  779. (fullcompare(tstringconstnode(p)) = 0))
  780. ;
  781. end;
  782. procedure tstringconstnode.changestringtype(def:tdef);
  783. const
  784. st2cst : array[tstringtype] of tconststringtype = (
  785. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  786. var
  787. pw : pcompilerwidestring;
  788. pc : pchar;
  789. cp1 : tstringencoding;
  790. cp2 : tstringencoding;
  791. l,l2 : longint;
  792. begin
  793. if def.typ<>stringdef then
  794. internalerror(200510011);
  795. { convert ascii 2 unicode }
  796. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  797. not(cst_type in [cst_widestring,cst_unicodestring]) then
  798. begin
  799. initwidestring(pw);
  800. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  801. ansistringdispose(value_str,len);
  802. pcompilerwidestring(value_str):=pw;
  803. end
  804. else
  805. { convert unicode 2 ascii }
  806. if (cst_type in [cst_widestring,cst_unicodestring]) and
  807. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  808. begin
  809. cp1:=tstringdef(def).encoding;
  810. if (cp1=globals.CP_NONE) or (cp1=0) then
  811. cp1:=current_settings.sourcecodepage;
  812. if (cp1=CP_UTF8) then
  813. begin
  814. pw:=pcompilerwidestring(value_str);
  815. l2:=len;
  816. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  817. getmem(pc,l);
  818. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  819. len:=l-1;
  820. donewidestring(pw);
  821. value_str:=pc;
  822. end
  823. else
  824. begin
  825. pw:=pcompilerwidestring(value_str);
  826. getmem(pc,getlengthwidestring(pw)+1);
  827. unicode2ascii(pw,pc,cp1);
  828. donewidestring(pw);
  829. value_str:=pc;
  830. end;
  831. end
  832. else
  833. if (tstringdef(def).stringtype = st_ansistring) and
  834. not(cst_type in [cst_widestring,cst_unicodestring]) then
  835. begin
  836. cp1:=tstringdef(def).encoding;
  837. if cp1=0 then
  838. cp1:=current_settings.sourcecodepage;
  839. if (cst_type = cst_ansistring) then
  840. begin
  841. cp2:=tstringdef(resultdef).encoding;
  842. if cp2=0 then
  843. cp2:=current_settings.sourcecodepage;
  844. end
  845. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  846. cp2:=current_settings.sourcecodepage
  847. else
  848. internalerror(2013112916);
  849. { don't change string if codepages are equal or string length is 0 }
  850. if (cp1<>cp2) and (len>0) then
  851. begin
  852. if cpavailable(cp1) and cpavailable(cp2) then
  853. changecodepage(value_str,len,cp2,value_str,cp1)
  854. else if (cp1 <> globals.CP_NONE) and (cp2 <> globals.CP_NONE) then
  855. begin
  856. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  857. if (cp2=CP_UTF8) then
  858. begin
  859. if not cpavailable(cp1) then
  860. Message1(option_code_page_not_available,IntToStr(cp1));
  861. initwidestring(pw);
  862. setlengthwidestring(pw,len);
  863. { returns room for terminating 0 }
  864. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  865. if (l<>getlengthwidestring(pw)) then
  866. begin
  867. setlengthwidestring(pw,l);
  868. ReAllocMem(value_str,l);
  869. end;
  870. unicode2ascii(pw,value_str,cp1);
  871. len:=l-1;
  872. donewidestring(pw);
  873. end
  874. else
  875. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  876. if (cp1=CP_UTF8) then
  877. begin
  878. if not cpavailable(cp2) then
  879. Message1(option_code_page_not_available,IntToStr(cp2));
  880. initwidestring(pw);
  881. setlengthwidestring(pw,len);
  882. ascii2unicode(value_str,len,cp2,pw);
  883. { returns room for terminating 0 }
  884. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  885. if l<>len then
  886. ReAllocMem(value_str,l);
  887. len:=l-1;
  888. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  889. donewidestring(pw);
  890. end
  891. else
  892. begin
  893. { output error message that encoding is not available for the compiler }
  894. if not cpavailable(cp1) then
  895. Message1(option_code_page_not_available,IntToStr(cp1));
  896. if not cpavailable(cp2) then
  897. Message1(option_code_page_not_available,IntToStr(cp2));
  898. end;
  899. end;
  900. end;
  901. end;
  902. cst_type:=st2cst[tstringdef(def).stringtype];
  903. resultdef:=def;
  904. end;
  905. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  906. begin
  907. if cst_type<>p.cst_type then
  908. InternalError(2009121701);
  909. if cst_type in [cst_widestring,cst_unicodestring] then
  910. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  911. else
  912. result:=compareansistrings(value_str,p.value_str,len,p.len);
  913. end;
  914. class function tstringconstnode.emptydynstrnil: boolean;
  915. begin
  916. result:=true;
  917. end;
  918. {*****************************************************************************
  919. TSETCONSTNODE
  920. *****************************************************************************}
  921. constructor tsetconstnode.create(s : pconstset;def:tdef);
  922. begin
  923. inherited create(setconstn,nil);
  924. typedef:=def;
  925. if assigned(s) then
  926. begin
  927. new(value_set);
  928. value_set^:=s^;
  929. end
  930. else
  931. value_set:=nil;
  932. end;
  933. destructor tsetconstnode.destroy;
  934. begin
  935. if assigned(value_set) then
  936. dispose(value_set);
  937. inherited destroy;
  938. end;
  939. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  940. begin
  941. inherited ppuload(t,ppufile);
  942. ppufile.getderef(typedefderef);
  943. new(value_set);
  944. ppufile.getnormalset(value_set^);
  945. end;
  946. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  947. begin
  948. inherited ppuwrite(ppufile);
  949. ppufile.putderef(typedefderef);
  950. ppufile.putnormalset(value_set^);
  951. end;
  952. procedure tsetconstnode.buildderefimpl;
  953. begin
  954. inherited buildderefimpl;
  955. typedefderef.build(typedef);
  956. end;
  957. procedure tsetconstnode.derefimpl;
  958. begin
  959. inherited derefimpl;
  960. typedef:=tdef(typedefderef.resolve);
  961. end;
  962. type
  963. setbytes = array[0..31] of byte;
  964. Psetbytes = ^setbytes;
  965. procedure tsetconstnode.adjustforsetbase;
  966. var
  967. i, diff: longint;
  968. begin
  969. { Internally, the compiler stores all sets with setbase 0, so we have }
  970. { to convert the set to its actual format in case setbase<>0 when }
  971. { writing it out }
  972. if (tsetdef(resultdef).setbase<>0) then
  973. begin
  974. if (tsetdef(resultdef).setbase and 7)<>0 then
  975. internalerror(2007091501);
  976. diff:=tsetdef(resultdef).setbase div 8;
  977. { This is endian-neutral in the new set format: in both cases, }
  978. { the first byte contains the first elements of the set. }
  979. { Since the compiler/base rtl cannot contain packed sets before }
  980. { they work for big endian, it's no problem that the code below }
  981. { is wrong for the old big endian set format (setbase cannot be }
  982. { <>0 with non-packed sets). }
  983. for i:=0 to tsetdef(resultdef).size-1 do
  984. begin
  985. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  986. Psetbytes(value_set)^[i+diff]:=0;
  987. end;
  988. end;
  989. end;
  990. function tsetconstnode.dogetcopy : tnode;
  991. var
  992. n : tsetconstnode;
  993. begin
  994. n:=tsetconstnode(inherited dogetcopy);
  995. if assigned(value_set) then
  996. begin
  997. new(n.value_set);
  998. n.value_set^:=value_set^
  999. end
  1000. else
  1001. n.value_set:=nil;
  1002. n.typedef := typedef;
  1003. n.lab_set:=lab_set;
  1004. dogetcopy:=n;
  1005. end;
  1006. function tsetconstnode.pass_typecheck:tnode;
  1007. begin
  1008. result:=nil;
  1009. resultdef:=typedef;
  1010. end;
  1011. function tsetconstnode.pass_1 : tnode;
  1012. begin
  1013. result:=nil;
  1014. if is_smallset(resultdef) then
  1015. expectloc:=LOC_CONSTANT
  1016. else
  1017. expectloc:=LOC_CREFERENCE;
  1018. if (cs_create_pic in current_settings.moduleswitches) and
  1019. (expectloc <> LOC_CONSTANT) then
  1020. include(current_procinfo.flags,pi_needs_got);
  1021. end;
  1022. function tsetconstnode.docompare(p: tnode): boolean;
  1023. begin
  1024. docompare:=(inherited docompare(p)) and
  1025. (value_set^=Tsetconstnode(p).value_set^);
  1026. end;
  1027. function tsetconstnode.elements : AInt;
  1028. var
  1029. i : longint;
  1030. begin
  1031. result:=0;
  1032. if not(assigned(value_set)) then
  1033. exit;
  1034. for i:=0 to tsetdef(resultdef).size-1 do
  1035. result:=result+ PopCnt(Psetbytes(value_set)^[i]);
  1036. end;
  1037. {*****************************************************************************
  1038. TNILNODE
  1039. *****************************************************************************}
  1040. constructor tnilnode.create;
  1041. begin
  1042. inherited create(niln);
  1043. end;
  1044. function tnilnode.pass_typecheck:tnode;
  1045. begin
  1046. result:=nil;
  1047. resultdef:=voidpointertype;
  1048. end;
  1049. function tnilnode.pass_1 : tnode;
  1050. begin
  1051. result:=nil;
  1052. expectloc:=LOC_CONSTANT;
  1053. end;
  1054. {*****************************************************************************
  1055. TGUIDCONSTNODE
  1056. *****************************************************************************}
  1057. constructor tguidconstnode.create(const g:tguid);
  1058. begin
  1059. inherited create(guidconstn);
  1060. value:=g;
  1061. end;
  1062. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1063. begin
  1064. inherited ppuload(t,ppufile);
  1065. ppufile.getguid(value);
  1066. end;
  1067. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1068. begin
  1069. inherited ppuwrite(ppufile);
  1070. ppufile.putguid(value);
  1071. end;
  1072. function tguidconstnode.dogetcopy : tnode;
  1073. var
  1074. n : tguidconstnode;
  1075. begin
  1076. n:=tguidconstnode(inherited dogetcopy);
  1077. n.value:=value;
  1078. n.lab_set:=lab_set;
  1079. dogetcopy:=n;
  1080. end;
  1081. function tguidconstnode.pass_typecheck:tnode;
  1082. begin
  1083. result:=nil;
  1084. resultdef:=rec_tguid;
  1085. end;
  1086. function tguidconstnode.pass_1 : tnode;
  1087. begin
  1088. result:=nil;
  1089. expectloc:=LOC_CREFERENCE;
  1090. if (cs_create_pic in current_settings.moduleswitches) and
  1091. (tf_pic_uses_got in target_info.flags) then
  1092. include(current_procinfo.flags,pi_needs_got);
  1093. end;
  1094. function tguidconstnode.docompare(p: tnode): boolean;
  1095. begin
  1096. docompare :=
  1097. inherited docompare(p) and
  1098. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1099. end;
  1100. end.