ncon.pas 42 KB

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