ncon.pas 37 KB

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