ncon.pas 36 KB

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