ncon.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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 createunistr(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 : tasmsymbol;
  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,procinfo,
  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.createunistr(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_refcountedstrings 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.createunistr(pcompilerwidestring(p.value.valueptr));
  281. constreal :
  282. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,p.constdef);
  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.typedef:=typedef;
  508. n.value_real:=value_real;
  509. n.value_currency:=value_currency;
  510. n.lab_real:=lab_real;
  511. dogetcopy:=n;
  512. end;
  513. function trealconstnode.pass_typecheck:tnode;
  514. begin
  515. result:=nil;
  516. resultdef:=typedef;
  517. end;
  518. function trealconstnode.pass_1 : tnode;
  519. begin
  520. result:=nil;
  521. expectloc:=LOC_CREFERENCE;
  522. if (cs_create_pic in current_settings.moduleswitches) then
  523. include(current_procinfo.flags,pi_needs_got);
  524. end;
  525. function trealconstnode.docompare(p: tnode): boolean;
  526. begin
  527. docompare :=
  528. inherited docompare(p) and
  529. { this should be always true }
  530. (trealconstnode(p).typedef.typ=floatdef) and (typedef.typ=floatdef) and
  531. (tfloatdef(typedef).floattype = tfloatdef(trealconstnode(p).typedef).floattype) and
  532. (
  533. (
  534. (tfloatdef(typedef).floattype=s64currency) and
  535. (value_currency=trealconstnode(p).value_currency)
  536. )
  537. or
  538. (
  539. (tfloatdef(typedef).floattype<>s64currency) and
  540. (value_real = trealconstnode(p).value_real) and
  541. { floating point compares for non-numbers give strange results usually }
  542. is_number_float(value_real) and
  543. is_number_float(trealconstnode(p).value_real)
  544. )
  545. );
  546. end;
  547. procedure Trealconstnode.printnodedata(var t:text);
  548. begin
  549. inherited printnodedata(t);
  550. writeln(t,printnodeindention,'value = ',value_real);
  551. end;
  552. {*****************************************************************************
  553. TORDCONSTNODE
  554. *****************************************************************************}
  555. constructor tordconstnode.create(v : tconstexprint;def:tdef;_rangecheck : boolean);
  556. begin
  557. inherited create(ordconstn);
  558. value:=v;
  559. typedef:=def;
  560. rangecheck := _rangecheck;
  561. end;
  562. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  563. begin
  564. inherited ppuload(t,ppufile);
  565. ppufile.getderef(typedefderef);
  566. value:=ppufile.getexprint;
  567. { normally, the value is already compiled, so we don't need
  568. to do once again a range check
  569. }
  570. rangecheck := false;
  571. end;
  572. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  573. begin
  574. inherited ppuwrite(ppufile);
  575. ppufile.putderef(typedefderef);
  576. ppufile.putexprint(value);
  577. end;
  578. procedure tordconstnode.buildderefimpl;
  579. begin
  580. inherited buildderefimpl;
  581. typedefderef.build(typedef);
  582. end;
  583. procedure tordconstnode.derefimpl;
  584. begin
  585. inherited derefimpl;
  586. typedef:=tdef(typedefderef.resolve);
  587. end;
  588. function tordconstnode.dogetcopy : tnode;
  589. var
  590. n : tordconstnode;
  591. begin
  592. n:=tordconstnode(inherited dogetcopy);
  593. n.value:=value;
  594. n.typedef := typedef;
  595. dogetcopy:=n;
  596. end;
  597. function tordconstnode.pass_typecheck:tnode;
  598. begin
  599. result:=nil;
  600. resultdef:=typedef;
  601. { only do range checking when explicitly asked for it
  602. and if the type can be range checked, see tests/tbs/tb0539.pp }
  603. if (resultdef.typ in [orddef,enumdef]) then
  604. testrange(resultdef,value,not rangecheck,false)
  605. end;
  606. function tordconstnode.pass_1 : tnode;
  607. begin
  608. result:=nil;
  609. expectloc:=LOC_CONSTANT;
  610. end;
  611. function tordconstnode.docompare(p: tnode): boolean;
  612. begin
  613. docompare :=
  614. inherited docompare(p) and
  615. (value = tordconstnode(p).value);
  616. end;
  617. procedure Tordconstnode.printnodedata(var t:text);
  618. begin
  619. inherited printnodedata(t);
  620. writeln(t,printnodeindention,'value = ',tostr(value));
  621. end;
  622. {*****************************************************************************
  623. TPOINTERCONSTNODE
  624. *****************************************************************************}
  625. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  626. begin
  627. inherited create(pointerconstn);
  628. value:=v;
  629. typedef:=def;
  630. end;
  631. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  632. begin
  633. inherited ppuload(t,ppufile);
  634. ppufile.getderef(typedefderef);
  635. value:=ppufile.getptruint;
  636. end;
  637. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  638. begin
  639. inherited ppuwrite(ppufile);
  640. ppufile.putderef(typedefderef);
  641. ppufile.putptruint(value);
  642. end;
  643. procedure tpointerconstnode.buildderefimpl;
  644. begin
  645. inherited buildderefimpl;
  646. typedefderef.build(typedef);
  647. end;
  648. procedure tpointerconstnode.derefimpl;
  649. begin
  650. inherited derefimpl;
  651. typedef:=tdef(typedefderef.resolve);
  652. end;
  653. function tpointerconstnode.dogetcopy : tnode;
  654. var
  655. n : tpointerconstnode;
  656. begin
  657. n:=tpointerconstnode(inherited dogetcopy);
  658. n.value:=value;
  659. n.typedef := typedef;
  660. dogetcopy:=n;
  661. end;
  662. function tpointerconstnode.pass_typecheck:tnode;
  663. begin
  664. result:=nil;
  665. resultdef:=typedef;
  666. end;
  667. function tpointerconstnode.pass_1 : tnode;
  668. begin
  669. result:=nil;
  670. expectloc:=LOC_CONSTANT;
  671. end;
  672. function tpointerconstnode.docompare(p: tnode): boolean;
  673. begin
  674. docompare :=
  675. inherited docompare(p) and
  676. (value = tpointerconstnode(p).value);
  677. end;
  678. {*****************************************************************************
  679. TSTRINGCONSTNODE
  680. *****************************************************************************}
  681. constructor tstringconstnode.createstr(const s : string);
  682. var
  683. l : longint;
  684. begin
  685. inherited create(stringconstn);
  686. l:=length(s);
  687. len:=l;
  688. { stringdup write even past a #0 }
  689. getmem(value_str,l+1);
  690. move(s[1],value_str^,l);
  691. value_str[l]:=#0;
  692. lab_str:=nil;
  693. cst_type:=cst_conststring;
  694. end;
  695. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  696. begin
  697. inherited create(stringconstn);
  698. len:=getlengthwidestring(w);
  699. initwidestring(pcompilerwidestring(value_str));
  700. copywidestring(w,pcompilerwidestring(value_str));
  701. lab_str:=nil;
  702. cst_type:=cst_unicodestring;
  703. end;
  704. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  705. begin
  706. inherited create(stringconstn);
  707. len:=l;
  708. value_str:=s;
  709. cst_type:=cst_conststring;
  710. lab_str:=nil;
  711. end;
  712. destructor tstringconstnode.destroy;
  713. begin
  714. if cst_type in [cst_widestring,cst_unicodestring] then
  715. donewidestring(pcompilerwidestring(value_str))
  716. else
  717. ansistringdispose(value_str,len);
  718. inherited destroy;
  719. end;
  720. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  721. var
  722. pw : pcompilerwidestring;
  723. i : longint;
  724. begin
  725. inherited ppuload(t,ppufile);
  726. cst_type:=tconststringtype(ppufile.getbyte);
  727. len:=ppufile.getlongint;
  728. if cst_type in [cst_widestring,cst_unicodestring] then
  729. begin
  730. initwidestring(pw);
  731. setlengthwidestring(pw,len);
  732. { don't use getdata, because the compilerwidechars may have to
  733. be byteswapped
  734. }
  735. {$if sizeof(tcompilerwidechar) = 2}
  736. for i:=0 to pw^.len-1 do
  737. pw^.data[i]:=ppufile.getword;
  738. {$elseif sizeof(tcompilerwidechar) = 4}
  739. for i:=0 to pw^.len-1 do
  740. pw^.data[i]:=cardinal(ppufile.getlongint);
  741. {$else}
  742. {$error Unsupported tcompilerwidechar size}
  743. {$endif}
  744. pcompilerwidestring(value_str):=pw
  745. end
  746. else
  747. begin
  748. getmem(value_str,len+1);
  749. ppufile.getdata(value_str^,len);
  750. value_str[len]:=#0;
  751. end;
  752. lab_str:=tasmlabel(ppufile.getasmsymbol);
  753. end;
  754. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  755. begin
  756. inherited ppuwrite(ppufile);
  757. ppufile.putbyte(byte(cst_type));
  758. ppufile.putlongint(len);
  759. if cst_type in [cst_widestring,cst_unicodestring] then
  760. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  761. else
  762. ppufile.putdata(value_str^,len);
  763. ppufile.putasmsymbol(lab_str);
  764. end;
  765. procedure tstringconstnode.buildderefimpl;
  766. begin
  767. inherited buildderefimpl;
  768. end;
  769. procedure tstringconstnode.derefimpl;
  770. begin
  771. inherited derefimpl;
  772. end;
  773. function tstringconstnode.dogetcopy : tnode;
  774. var
  775. n : tstringconstnode;
  776. begin
  777. n:=tstringconstnode(inherited dogetcopy);
  778. n.cst_type:=cst_type;
  779. n.len:=len;
  780. n.lab_str:=lab_str;
  781. if cst_type in [cst_widestring,cst_unicodestring] then
  782. begin
  783. initwidestring(pcompilerwidestring(n.value_str));
  784. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  785. end
  786. else
  787. n.value_str:=getpcharcopy;
  788. dogetcopy:=n;
  789. end;
  790. function tstringconstnode.pass_typecheck:tnode;
  791. var
  792. l : aint;
  793. begin
  794. result:=nil;
  795. case cst_type of
  796. cst_conststring :
  797. begin
  798. { handle and store as array[0..len-1] of char }
  799. if len>0 then
  800. l:=len-1
  801. else
  802. l:=0;
  803. resultdef:=tarraydef.create(0,l,s32inttype);
  804. tarraydef(resultdef).elementdef:=cansichartype;
  805. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  806. end;
  807. cst_shortstring :
  808. resultdef:=cshortstringtype;
  809. cst_ansistring :
  810. resultdef:=getansistringdef;
  811. cst_unicodestring :
  812. resultdef:=cunicodestringtype;
  813. cst_widestring :
  814. resultdef:=cwidestringtype;
  815. cst_longstring :
  816. resultdef:=clongstringtype;
  817. end;
  818. end;
  819. function tstringconstnode.pass_1 : tnode;
  820. begin
  821. result:=nil;
  822. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  823. begin
  824. if len=0 then
  825. expectloc:=LOC_CONSTANT
  826. else
  827. expectloc:=LOC_REGISTER
  828. end
  829. else
  830. expectloc:=LOC_CREFERENCE;
  831. if (cs_create_pic in current_settings.moduleswitches) and
  832. (expectloc <> LOC_CONSTANT) then
  833. include(current_procinfo.flags,pi_needs_got);
  834. end;
  835. function tstringconstnode.getpcharcopy : pchar;
  836. var
  837. pc : pchar;
  838. begin
  839. pc:=nil;
  840. getmem(pc,len+1);
  841. if pc=nil then
  842. Message(general_f_no_memory_left);
  843. move(value_str^,pc^,len+1);
  844. getpcharcopy:=pc;
  845. end;
  846. function tstringconstnode.docompare(p: tnode): boolean;
  847. begin
  848. docompare :=
  849. inherited docompare(p) and
  850. (len = tstringconstnode(p).len) and
  851. (lab_str = tstringconstnode(p).lab_str) and
  852. { This is enough as soon as labels are allocated, otherwise }
  853. { fall back to content compare. }
  854. (assigned(lab_str) or
  855. (cst_type = tstringconstnode(p).cst_type) and
  856. (fullcompare(tstringconstnode(p)) = 0))
  857. ;
  858. end;
  859. procedure tstringconstnode.changestringtype(def:tdef);
  860. const
  861. st2cst : array[tstringtype] of tconststringtype = (
  862. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  863. var
  864. pw : pcompilerwidestring;
  865. pc : pchar;
  866. cp1 : tstringencoding;
  867. cp2 : tstringencoding;
  868. l,l2 : longint;
  869. begin
  870. if def.typ<>stringdef then
  871. internalerror(200510011);
  872. { convert ascii 2 unicode }
  873. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  874. not(cst_type in [cst_widestring,cst_unicodestring]) then
  875. begin
  876. initwidestring(pw);
  877. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  878. ansistringdispose(value_str,len);
  879. pcompilerwidestring(value_str):=pw;
  880. end
  881. else
  882. { convert unicode 2 ascii }
  883. if (cst_type in [cst_widestring,cst_unicodestring]) and
  884. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  885. begin
  886. cp1:=tstringdef(def).encoding;
  887. if (cp1=CP_NONE) or (cp1=0) then
  888. cp1:=current_settings.sourcecodepage;
  889. if (cp1=CP_UTF8) then
  890. begin
  891. pw:=pcompilerwidestring(value_str);
  892. l2:=len;
  893. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  894. getmem(pc,l);
  895. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  896. len:=l-1;
  897. donewidestring(pw);
  898. value_str:=pc;
  899. end
  900. else
  901. begin
  902. pw:=pcompilerwidestring(value_str);
  903. getmem(pc,getlengthwidestring(pw)+1);
  904. unicode2ascii(pw,pc,cp1);
  905. donewidestring(pw);
  906. value_str:=pc;
  907. end;
  908. end
  909. else
  910. if (tstringdef(def).stringtype = st_ansistring) and
  911. not(cst_type in [cst_widestring,cst_unicodestring]) then
  912. begin
  913. cp1:=tstringdef(def).encoding;
  914. if cp1=0 then
  915. cp1:=current_settings.sourcecodepage;
  916. if (cst_type = cst_ansistring) then
  917. begin
  918. cp2:=tstringdef(resultdef).encoding;
  919. if cp2=0 then
  920. cp2:=current_settings.sourcecodepage;
  921. end
  922. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  923. cp2:=current_settings.sourcecodepage;
  924. { don't change string if codepages are equal or string length is 0 }
  925. if (cp1<>cp2) and (len>0) then
  926. begin
  927. if cpavailable(cp1) and cpavailable(cp2) then
  928. changecodepage(value_str,len,cp2,value_str,cp1)
  929. else if (cp1 <> CP_NONE) and (cp2 <> CP_NONE) then
  930. begin
  931. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  932. if (cp2=CP_UTF8) then
  933. begin
  934. if not cpavailable(cp1) then
  935. Message1(option_code_page_not_available,IntToStr(cp1));
  936. initwidestring(pw);
  937. setlengthwidestring(pw,len);
  938. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  939. if (l<>getlengthwidestring(pw)) then
  940. begin
  941. setlengthwidestring(pw,l);
  942. ReAllocMem(value_str,l);
  943. end;
  944. unicode2ascii(pw,value_str,cp1);
  945. donewidestring(pw);
  946. end
  947. else
  948. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  949. if (cp1=CP_UTF8) then
  950. begin
  951. if not cpavailable(cp2) then
  952. Message1(option_code_page_not_available,IntToStr(cp2));
  953. initwidestring(pw);
  954. setlengthwidestring(pw,len);
  955. ascii2unicode(value_str,len,cp2,pw);
  956. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  957. if l<>len then
  958. ReAllocMem(value_str,l);
  959. len:=l-1;
  960. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  961. donewidestring(pw);
  962. end
  963. else
  964. begin
  965. { output error message that encoding is not available for the compiler }
  966. if not cpavailable(cp1) then
  967. Message1(option_code_page_not_available,IntToStr(cp1));
  968. if not cpavailable(cp2) then
  969. Message1(option_code_page_not_available,IntToStr(cp2));
  970. end;
  971. end;
  972. end;
  973. end;
  974. cst_type:=st2cst[tstringdef(def).stringtype];
  975. resultdef:=def;
  976. end;
  977. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  978. begin
  979. if cst_type<>p.cst_type then
  980. InternalError(2009121701);
  981. if cst_type in [cst_widestring,cst_unicodestring] then
  982. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  983. else
  984. result:=compareansistrings(value_str,p.value_str,len,p.len);
  985. end;
  986. {*****************************************************************************
  987. TSETCONSTNODE
  988. *****************************************************************************}
  989. constructor tsetconstnode.create(s : pconstset;def:tdef);
  990. begin
  991. inherited create(setconstn,nil);
  992. typedef:=def;
  993. if assigned(s) then
  994. begin
  995. new(value_set);
  996. value_set^:=s^;
  997. end
  998. else
  999. value_set:=nil;
  1000. end;
  1001. destructor tsetconstnode.destroy;
  1002. begin
  1003. if assigned(value_set) then
  1004. dispose(value_set);
  1005. inherited destroy;
  1006. end;
  1007. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1008. begin
  1009. inherited ppuload(t,ppufile);
  1010. ppufile.getderef(typedefderef);
  1011. new(value_set);
  1012. ppufile.getnormalset(value_set^);
  1013. end;
  1014. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1015. begin
  1016. inherited ppuwrite(ppufile);
  1017. ppufile.putderef(typedefderef);
  1018. ppufile.putnormalset(value_set^);
  1019. end;
  1020. procedure tsetconstnode.buildderefimpl;
  1021. begin
  1022. inherited buildderefimpl;
  1023. typedefderef.build(typedef);
  1024. end;
  1025. procedure tsetconstnode.derefimpl;
  1026. begin
  1027. inherited derefimpl;
  1028. typedef:=tdef(typedefderef.resolve);
  1029. end;
  1030. procedure tsetconstnode.adjustforsetbase;
  1031. type
  1032. setbytes = array[0..31] of byte;
  1033. Psetbytes = ^setbytes;
  1034. var
  1035. i, diff: longint;
  1036. begin
  1037. { Internally, the compiler stores all sets with setbase 0, so we have }
  1038. { to convert the set to its actual format in case setbase<>0 when }
  1039. { writing it out }
  1040. if (tsetdef(resultdef).setbase<>0) then
  1041. begin
  1042. if (tsetdef(resultdef).setbase and 7)<>0 then
  1043. internalerror(2007091501);
  1044. diff:=tsetdef(resultdef).setbase div 8;
  1045. { This is endian-neutral in the new set format: in both cases, }
  1046. { the first byte contains the first elements of the set. }
  1047. { Since the compiler/base rtl cannot contain packed sets before }
  1048. { they work for big endian, it's no problem that the code below }
  1049. { is wrong for the old big endian set format (setbase cannot be }
  1050. { <>0 with non-packed sets). }
  1051. for i:=0 to tsetdef(resultdef).size-1 do
  1052. begin
  1053. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1054. Psetbytes(value_set)^[i+diff]:=0;
  1055. end;
  1056. end;
  1057. end;
  1058. function tsetconstnode.dogetcopy : tnode;
  1059. var
  1060. n : tsetconstnode;
  1061. begin
  1062. n:=tsetconstnode(inherited dogetcopy);
  1063. if assigned(value_set) then
  1064. begin
  1065. new(n.value_set);
  1066. n.value_set^:=value_set^
  1067. end
  1068. else
  1069. n.value_set:=nil;
  1070. n.typedef := typedef;
  1071. n.lab_set:=lab_set;
  1072. dogetcopy:=n;
  1073. end;
  1074. function tsetconstnode.pass_typecheck:tnode;
  1075. begin
  1076. result:=nil;
  1077. resultdef:=typedef;
  1078. end;
  1079. function tsetconstnode.pass_1 : tnode;
  1080. begin
  1081. result:=nil;
  1082. if is_smallset(resultdef) then
  1083. expectloc:=LOC_CONSTANT
  1084. else
  1085. expectloc:=LOC_CREFERENCE;
  1086. if (cs_create_pic in current_settings.moduleswitches) and
  1087. (expectloc <> LOC_CONSTANT) then
  1088. include(current_procinfo.flags,pi_needs_got);
  1089. end;
  1090. function tsetconstnode.docompare(p: tnode): boolean;
  1091. begin
  1092. docompare:=(inherited docompare(p)) and
  1093. (value_set^=Tsetconstnode(p).value_set^);
  1094. end;
  1095. {*****************************************************************************
  1096. TNILNODE
  1097. *****************************************************************************}
  1098. constructor tnilnode.create;
  1099. begin
  1100. inherited create(niln);
  1101. end;
  1102. function tnilnode.pass_typecheck:tnode;
  1103. begin
  1104. result:=nil;
  1105. resultdef:=voidpointertype;
  1106. end;
  1107. function tnilnode.pass_1 : tnode;
  1108. begin
  1109. result:=nil;
  1110. expectloc:=LOC_CONSTANT;
  1111. end;
  1112. {*****************************************************************************
  1113. TGUIDCONSTNODE
  1114. *****************************************************************************}
  1115. constructor tguidconstnode.create(const g:tguid);
  1116. begin
  1117. inherited create(guidconstn);
  1118. value:=g;
  1119. end;
  1120. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1121. begin
  1122. inherited ppuload(t,ppufile);
  1123. ppufile.getguid(value);
  1124. end;
  1125. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1126. begin
  1127. inherited ppuwrite(ppufile);
  1128. ppufile.putguid(value);
  1129. end;
  1130. function tguidconstnode.dogetcopy : tnode;
  1131. var
  1132. n : tguidconstnode;
  1133. begin
  1134. n:=tguidconstnode(inherited dogetcopy);
  1135. n.value:=value;
  1136. dogetcopy:=n;
  1137. end;
  1138. function tguidconstnode.pass_typecheck:tnode;
  1139. begin
  1140. result:=nil;
  1141. resultdef:=rec_tguid;
  1142. end;
  1143. function tguidconstnode.pass_1 : tnode;
  1144. begin
  1145. result:=nil;
  1146. expectloc:=LOC_CREFERENCE;
  1147. if (cs_create_pic in current_settings.moduleswitches) and
  1148. (tf_pic_uses_got in target_info.flags) then
  1149. include(current_procinfo.flags,pi_needs_got);
  1150. end;
  1151. function tguidconstnode.docompare(p: tnode): boolean;
  1152. begin
  1153. docompare :=
  1154. inherited docompare(p) and
  1155. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1156. end;
  1157. end.