ncon.pas 41 KB

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