ncon.pas 40 KB

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