2
0

ncon.pas 37 KB

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