ncon.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. {$ifdef i8086}
  641. { truncate near pointers }
  642. if (def.typ<>pointerdef) or not (tpointerdef(def).x86pointertyp in [x86pt_far,x86pt_huge]) then
  643. v := Word(v);
  644. {$endif i8086}
  645. value:=v;
  646. typedef:=def;
  647. end;
  648. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  649. begin
  650. inherited ppuload(t,ppufile);
  651. ppufile.getderef(typedefderef);
  652. value:=ppufile.getptruint;
  653. end;
  654. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  655. begin
  656. inherited ppuwrite(ppufile);
  657. ppufile.putderef(typedefderef);
  658. ppufile.putptruint(value);
  659. end;
  660. procedure tpointerconstnode.buildderefimpl;
  661. begin
  662. inherited buildderefimpl;
  663. typedefderef.build(typedef);
  664. end;
  665. procedure tpointerconstnode.derefimpl;
  666. begin
  667. inherited derefimpl;
  668. typedef:=tdef(typedefderef.resolve);
  669. end;
  670. function tpointerconstnode.dogetcopy : tnode;
  671. var
  672. n : tpointerconstnode;
  673. begin
  674. n:=tpointerconstnode(inherited dogetcopy);
  675. n.value:=value;
  676. n.typedef := typedef;
  677. dogetcopy:=n;
  678. end;
  679. function tpointerconstnode.pass_typecheck:tnode;
  680. begin
  681. result:=nil;
  682. resultdef:=typedef;
  683. end;
  684. function tpointerconstnode.pass_1 : tnode;
  685. begin
  686. result:=nil;
  687. expectloc:=LOC_CONSTANT;
  688. end;
  689. function tpointerconstnode.docompare(p: tnode): boolean;
  690. begin
  691. docompare :=
  692. inherited docompare(p) and
  693. (value = tpointerconstnode(p).value);
  694. end;
  695. {*****************************************************************************
  696. TSTRINGCONSTNODE
  697. *****************************************************************************}
  698. constructor tstringconstnode.createstr(const s : string);
  699. var
  700. l : longint;
  701. begin
  702. inherited create(stringconstn);
  703. l:=length(s);
  704. len:=l;
  705. { stringdup write even past a #0 }
  706. getmem(value_str,l+1);
  707. move(s[1],value_str^,l);
  708. value_str[l]:=#0;
  709. lab_str:=nil;
  710. cst_type:=cst_conststring;
  711. end;
  712. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  713. begin
  714. inherited create(stringconstn);
  715. len:=getlengthwidestring(w);
  716. initwidestring(pcompilerwidestring(value_str));
  717. copywidestring(w,pcompilerwidestring(value_str));
  718. lab_str:=nil;
  719. cst_type:=cst_unicodestring;
  720. end;
  721. constructor tstringconstnode.createpchar(s: pchar; l: longint; def: tdef);
  722. begin
  723. inherited create(stringconstn);
  724. len:=l;
  725. value_str:=s;
  726. if assigned(def) and
  727. is_ansistring(def) then
  728. begin
  729. cst_type:=cst_ansistring;
  730. astringdef:=def;
  731. end
  732. else
  733. cst_type:=cst_conststring;
  734. lab_str:=nil;
  735. end;
  736. destructor tstringconstnode.destroy;
  737. begin
  738. if cst_type in [cst_widestring,cst_unicodestring] then
  739. donewidestring(pcompilerwidestring(value_str))
  740. else
  741. ansistringdispose(value_str,len);
  742. inherited destroy;
  743. end;
  744. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  745. var
  746. pw : pcompilerwidestring;
  747. i : longint;
  748. begin
  749. inherited ppuload(t,ppufile);
  750. cst_type:=tconststringtype(ppufile.getbyte);
  751. len:=ppufile.getlongint;
  752. if cst_type in [cst_widestring,cst_unicodestring] then
  753. begin
  754. initwidestring(pw);
  755. setlengthwidestring(pw,len);
  756. { don't use getdata, because the compilerwidechars may have to
  757. be byteswapped
  758. }
  759. {$if sizeof(tcompilerwidechar) = 2}
  760. for i:=0 to pw^.len-1 do
  761. pw^.data[i]:=ppufile.getword;
  762. {$elseif sizeof(tcompilerwidechar) = 4}
  763. for i:=0 to pw^.len-1 do
  764. pw^.data[i]:=cardinal(ppufile.getlongint);
  765. {$else}
  766. {$error Unsupported tcompilerwidechar size}
  767. {$endif}
  768. pcompilerwidestring(value_str):=pw
  769. end
  770. else
  771. begin
  772. getmem(value_str,len+1);
  773. ppufile.getdata(value_str^,len);
  774. value_str[len]:=#0;
  775. end;
  776. lab_str:=tasmlabel(ppufile.getasmsymbol);
  777. if cst_type=cst_ansistring then
  778. ppufile.getderef(astringdefderef);
  779. end;
  780. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  781. begin
  782. inherited ppuwrite(ppufile);
  783. ppufile.putbyte(byte(cst_type));
  784. ppufile.putlongint(len);
  785. if cst_type in [cst_widestring,cst_unicodestring] then
  786. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  787. else
  788. ppufile.putdata(value_str^,len);
  789. ppufile.putasmsymbol(lab_str);
  790. if cst_type=cst_ansistring then
  791. ppufile.putderef(astringdefderef);
  792. end;
  793. procedure tstringconstnode.buildderefimpl;
  794. begin
  795. inherited buildderefimpl;
  796. if cst_type=cst_ansistring then
  797. astringdefderef.build(astringdef);
  798. end;
  799. procedure tstringconstnode.derefimpl;
  800. begin
  801. inherited derefimpl;
  802. if cst_type=cst_ansistring then
  803. astringdef:=tdef(astringdefderef.resolve);
  804. end;
  805. function tstringconstnode.dogetcopy : tnode;
  806. var
  807. n : tstringconstnode;
  808. begin
  809. n:=tstringconstnode(inherited dogetcopy);
  810. n.cst_type:=cst_type;
  811. n.len:=len;
  812. n.lab_str:=lab_str;
  813. if cst_type in [cst_widestring,cst_unicodestring] then
  814. begin
  815. initwidestring(pcompilerwidestring(n.value_str));
  816. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  817. end
  818. else
  819. n.value_str:=getpcharcopy;
  820. n.astringdef:=astringdef;
  821. dogetcopy:=n;
  822. end;
  823. function tstringconstnode.pass_typecheck:tnode;
  824. var
  825. l : aint;
  826. begin
  827. result:=nil;
  828. case cst_type of
  829. cst_conststring :
  830. begin
  831. { handle and store as array[0..len-1] of char }
  832. if len>0 then
  833. l:=len-1
  834. else
  835. l:=0;
  836. resultdef:=tarraydef.create(0,l,s32inttype);
  837. tarraydef(resultdef).elementdef:=cansichartype;
  838. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  839. end;
  840. cst_shortstring :
  841. resultdef:=cshortstringtype;
  842. cst_ansistring :
  843. if not assigned(astringdef) then
  844. resultdef:=getansistringdef
  845. else
  846. resultdef:=astringdef;
  847. cst_unicodestring :
  848. resultdef:=cunicodestringtype;
  849. cst_widestring :
  850. resultdef:=cwidestringtype;
  851. cst_longstring :
  852. resultdef:=clongstringtype;
  853. end;
  854. end;
  855. function tstringconstnode.pass_1 : tnode;
  856. begin
  857. result:=nil;
  858. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  859. begin
  860. if len=0 then
  861. expectloc:=LOC_CONSTANT
  862. else
  863. expectloc:=LOC_REGISTER
  864. end
  865. else
  866. expectloc:=LOC_CREFERENCE;
  867. if (cs_create_pic in current_settings.moduleswitches) and
  868. (expectloc <> LOC_CONSTANT) then
  869. include(current_procinfo.flags,pi_needs_got);
  870. end;
  871. function tstringconstnode.getpcharcopy : pchar;
  872. var
  873. pc : pchar;
  874. begin
  875. pc:=nil;
  876. getmem(pc,len+1);
  877. if pc=nil then
  878. Message(general_f_no_memory_left);
  879. move(value_str^,pc^,len+1);
  880. getpcharcopy:=pc;
  881. end;
  882. function tstringconstnode.docompare(p: tnode): boolean;
  883. begin
  884. docompare :=
  885. inherited docompare(p) and
  886. (len = tstringconstnode(p).len) and
  887. (lab_str = tstringconstnode(p).lab_str) and
  888. { This is enough as soon as labels are allocated, otherwise }
  889. { fall back to content compare. }
  890. (assigned(lab_str) or
  891. (cst_type = tstringconstnode(p).cst_type) and
  892. (fullcompare(tstringconstnode(p)) = 0))
  893. ;
  894. end;
  895. procedure tstringconstnode.changestringtype(def:tdef);
  896. const
  897. st2cst : array[tstringtype] of tconststringtype = (
  898. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  899. var
  900. pw : pcompilerwidestring;
  901. pc : pchar;
  902. cp1 : tstringencoding;
  903. cp2 : tstringencoding;
  904. l,l2 : longint;
  905. begin
  906. if def.typ<>stringdef then
  907. internalerror(200510011);
  908. { convert ascii 2 unicode }
  909. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  910. not(cst_type in [cst_widestring,cst_unicodestring]) then
  911. begin
  912. initwidestring(pw);
  913. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  914. ansistringdispose(value_str,len);
  915. pcompilerwidestring(value_str):=pw;
  916. end
  917. else
  918. { convert unicode 2 ascii }
  919. if (cst_type in [cst_widestring,cst_unicodestring]) and
  920. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  921. begin
  922. cp1:=tstringdef(def).encoding;
  923. if (cp1=globals.CP_NONE) or (cp1=0) then
  924. cp1:=current_settings.sourcecodepage;
  925. if (cp1=CP_UTF8) then
  926. begin
  927. pw:=pcompilerwidestring(value_str);
  928. l2:=len;
  929. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  930. getmem(pc,l);
  931. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  932. len:=l-1;
  933. donewidestring(pw);
  934. value_str:=pc;
  935. end
  936. else
  937. begin
  938. pw:=pcompilerwidestring(value_str);
  939. getmem(pc,getlengthwidestring(pw)+1);
  940. unicode2ascii(pw,pc,cp1);
  941. donewidestring(pw);
  942. value_str:=pc;
  943. end;
  944. end
  945. else
  946. if (tstringdef(def).stringtype = st_ansistring) and
  947. not(cst_type in [cst_widestring,cst_unicodestring]) then
  948. begin
  949. cp1:=tstringdef(def).encoding;
  950. if cp1=0 then
  951. cp1:=current_settings.sourcecodepage;
  952. if (cst_type = cst_ansistring) then
  953. begin
  954. cp2:=tstringdef(resultdef).encoding;
  955. if cp2=0 then
  956. cp2:=current_settings.sourcecodepage;
  957. end
  958. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  959. cp2:=current_settings.sourcecodepage;
  960. { don't change string if codepages are equal or string length is 0 }
  961. if (cp1<>cp2) and (len>0) then
  962. begin
  963. if cpavailable(cp1) and cpavailable(cp2) then
  964. changecodepage(value_str,len,cp2,value_str,cp1)
  965. else if (cp1 <> globals.CP_NONE) and (cp2 <> globals.CP_NONE) then
  966. begin
  967. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  968. if (cp2=CP_UTF8) then
  969. begin
  970. if not cpavailable(cp1) then
  971. Message1(option_code_page_not_available,IntToStr(cp1));
  972. initwidestring(pw);
  973. setlengthwidestring(pw,len);
  974. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  975. if (l<>getlengthwidestring(pw)) then
  976. begin
  977. setlengthwidestring(pw,l);
  978. ReAllocMem(value_str,l);
  979. end;
  980. unicode2ascii(pw,value_str,cp1);
  981. donewidestring(pw);
  982. end
  983. else
  984. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  985. if (cp1=CP_UTF8) then
  986. begin
  987. if not cpavailable(cp2) then
  988. Message1(option_code_page_not_available,IntToStr(cp2));
  989. initwidestring(pw);
  990. setlengthwidestring(pw,len);
  991. ascii2unicode(value_str,len,cp2,pw);
  992. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  993. if l<>len then
  994. ReAllocMem(value_str,l);
  995. len:=l-1;
  996. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  997. donewidestring(pw);
  998. end
  999. else
  1000. begin
  1001. { output error message that encoding is not available for the compiler }
  1002. if not cpavailable(cp1) then
  1003. Message1(option_code_page_not_available,IntToStr(cp1));
  1004. if not cpavailable(cp2) then
  1005. Message1(option_code_page_not_available,IntToStr(cp2));
  1006. end;
  1007. end;
  1008. end;
  1009. end;
  1010. cst_type:=st2cst[tstringdef(def).stringtype];
  1011. resultdef:=def;
  1012. end;
  1013. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  1014. begin
  1015. if cst_type<>p.cst_type then
  1016. InternalError(2009121701);
  1017. if cst_type in [cst_widestring,cst_unicodestring] then
  1018. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  1019. else
  1020. result:=compareansistrings(value_str,p.value_str,len,p.len);
  1021. end;
  1022. {*****************************************************************************
  1023. TSETCONSTNODE
  1024. *****************************************************************************}
  1025. constructor tsetconstnode.create(s : pconstset;def:tdef);
  1026. begin
  1027. inherited create(setconstn,nil);
  1028. typedef:=def;
  1029. if assigned(s) then
  1030. begin
  1031. new(value_set);
  1032. value_set^:=s^;
  1033. end
  1034. else
  1035. value_set:=nil;
  1036. end;
  1037. destructor tsetconstnode.destroy;
  1038. begin
  1039. if assigned(value_set) then
  1040. dispose(value_set);
  1041. inherited destroy;
  1042. end;
  1043. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1044. begin
  1045. inherited ppuload(t,ppufile);
  1046. ppufile.getderef(typedefderef);
  1047. new(value_set);
  1048. ppufile.getnormalset(value_set^);
  1049. end;
  1050. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1051. begin
  1052. inherited ppuwrite(ppufile);
  1053. ppufile.putderef(typedefderef);
  1054. ppufile.putnormalset(value_set^);
  1055. end;
  1056. procedure tsetconstnode.buildderefimpl;
  1057. begin
  1058. inherited buildderefimpl;
  1059. typedefderef.build(typedef);
  1060. end;
  1061. procedure tsetconstnode.derefimpl;
  1062. begin
  1063. inherited derefimpl;
  1064. typedef:=tdef(typedefderef.resolve);
  1065. end;
  1066. procedure tsetconstnode.adjustforsetbase;
  1067. type
  1068. setbytes = array[0..31] of byte;
  1069. Psetbytes = ^setbytes;
  1070. var
  1071. i, diff: longint;
  1072. begin
  1073. { Internally, the compiler stores all sets with setbase 0, so we have }
  1074. { to convert the set to its actual format in case setbase<>0 when }
  1075. { writing it out }
  1076. if (tsetdef(resultdef).setbase<>0) then
  1077. begin
  1078. if (tsetdef(resultdef).setbase and 7)<>0 then
  1079. internalerror(2007091501);
  1080. diff:=tsetdef(resultdef).setbase div 8;
  1081. { This is endian-neutral in the new set format: in both cases, }
  1082. { the first byte contains the first elements of the set. }
  1083. { Since the compiler/base rtl cannot contain packed sets before }
  1084. { they work for big endian, it's no problem that the code below }
  1085. { is wrong for the old big endian set format (setbase cannot be }
  1086. { <>0 with non-packed sets). }
  1087. for i:=0 to tsetdef(resultdef).size-1 do
  1088. begin
  1089. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1090. Psetbytes(value_set)^[i+diff]:=0;
  1091. end;
  1092. end;
  1093. end;
  1094. function tsetconstnode.dogetcopy : tnode;
  1095. var
  1096. n : tsetconstnode;
  1097. begin
  1098. n:=tsetconstnode(inherited dogetcopy);
  1099. if assigned(value_set) then
  1100. begin
  1101. new(n.value_set);
  1102. n.value_set^:=value_set^
  1103. end
  1104. else
  1105. n.value_set:=nil;
  1106. n.typedef := typedef;
  1107. n.lab_set:=lab_set;
  1108. dogetcopy:=n;
  1109. end;
  1110. function tsetconstnode.pass_typecheck:tnode;
  1111. begin
  1112. result:=nil;
  1113. resultdef:=typedef;
  1114. end;
  1115. function tsetconstnode.pass_1 : tnode;
  1116. begin
  1117. result:=nil;
  1118. if is_smallset(resultdef) then
  1119. expectloc:=LOC_CONSTANT
  1120. else
  1121. expectloc:=LOC_CREFERENCE;
  1122. if (cs_create_pic in current_settings.moduleswitches) and
  1123. (expectloc <> LOC_CONSTANT) then
  1124. include(current_procinfo.flags,pi_needs_got);
  1125. end;
  1126. function tsetconstnode.docompare(p: tnode): boolean;
  1127. begin
  1128. docompare:=(inherited docompare(p)) and
  1129. (value_set^=Tsetconstnode(p).value_set^);
  1130. end;
  1131. {*****************************************************************************
  1132. TNILNODE
  1133. *****************************************************************************}
  1134. constructor tnilnode.create;
  1135. begin
  1136. inherited create(niln);
  1137. end;
  1138. function tnilnode.pass_typecheck:tnode;
  1139. begin
  1140. result:=nil;
  1141. resultdef:=voidpointertype;
  1142. end;
  1143. function tnilnode.pass_1 : tnode;
  1144. begin
  1145. result:=nil;
  1146. expectloc:=LOC_CONSTANT;
  1147. end;
  1148. {*****************************************************************************
  1149. TGUIDCONSTNODE
  1150. *****************************************************************************}
  1151. constructor tguidconstnode.create(const g:tguid);
  1152. begin
  1153. inherited create(guidconstn);
  1154. value:=g;
  1155. end;
  1156. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1157. begin
  1158. inherited ppuload(t,ppufile);
  1159. ppufile.getguid(value);
  1160. end;
  1161. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1162. begin
  1163. inherited ppuwrite(ppufile);
  1164. ppufile.putguid(value);
  1165. end;
  1166. function tguidconstnode.dogetcopy : tnode;
  1167. var
  1168. n : tguidconstnode;
  1169. begin
  1170. n:=tguidconstnode(inherited dogetcopy);
  1171. n.value:=value;
  1172. n.lab_set:=lab_set;
  1173. dogetcopy:=n;
  1174. end;
  1175. function tguidconstnode.pass_typecheck:tnode;
  1176. begin
  1177. result:=nil;
  1178. resultdef:=rec_tguid;
  1179. end;
  1180. function tguidconstnode.pass_1 : tnode;
  1181. begin
  1182. result:=nil;
  1183. expectloc:=LOC_CREFERENCE;
  1184. if (cs_create_pic in current_settings.moduleswitches) and
  1185. (tf_pic_uses_got in target_info.flags) then
  1186. include(current_procinfo.flags,pi_needs_got);
  1187. end;
  1188. function tguidconstnode.docompare(p: tnode): boolean;
  1189. begin
  1190. docompare :=
  1191. inherited docompare(p) and
  1192. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1193. end;
  1194. end.