ncon.pas 37 KB

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