ncon.pas 42 KB

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