ncon.pas 40 KB

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