ncon.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. {$ifdef i8086}
  630. { truncate near pointers }
  631. if (def.typ<>pointerdef) or not (tpointerdef(def).x86pointertyp in [x86pt_far,x86pt_huge]) then
  632. v := Word(v);
  633. {$endif i8086}
  634. value:=v;
  635. typedef:=def;
  636. end;
  637. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  638. begin
  639. inherited ppuload(t,ppufile);
  640. ppufile.getderef(typedefderef);
  641. value:=ppufile.getptruint;
  642. end;
  643. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  644. begin
  645. inherited ppuwrite(ppufile);
  646. ppufile.putderef(typedefderef);
  647. ppufile.putptruint(value);
  648. end;
  649. procedure tpointerconstnode.buildderefimpl;
  650. begin
  651. inherited buildderefimpl;
  652. typedefderef.build(typedef);
  653. end;
  654. procedure tpointerconstnode.derefimpl;
  655. begin
  656. inherited derefimpl;
  657. typedef:=tdef(typedefderef.resolve);
  658. end;
  659. function tpointerconstnode.dogetcopy : tnode;
  660. var
  661. n : tpointerconstnode;
  662. begin
  663. n:=tpointerconstnode(inherited dogetcopy);
  664. n.value:=value;
  665. n.typedef := typedef;
  666. dogetcopy:=n;
  667. end;
  668. function tpointerconstnode.pass_typecheck:tnode;
  669. begin
  670. result:=nil;
  671. resultdef:=typedef;
  672. end;
  673. function tpointerconstnode.pass_1 : tnode;
  674. begin
  675. result:=nil;
  676. expectloc:=LOC_CONSTANT;
  677. end;
  678. function tpointerconstnode.docompare(p: tnode): boolean;
  679. begin
  680. docompare :=
  681. inherited docompare(p) and
  682. (value = tpointerconstnode(p).value);
  683. end;
  684. {*****************************************************************************
  685. TSTRINGCONSTNODE
  686. *****************************************************************************}
  687. constructor tstringconstnode.createstr(const s : string);
  688. var
  689. l : longint;
  690. begin
  691. inherited create(stringconstn);
  692. l:=length(s);
  693. len:=l;
  694. { stringdup write even past a #0 }
  695. getmem(value_str,l+1);
  696. move(s[1],value_str^,l);
  697. value_str[l]:=#0;
  698. lab_str:=nil;
  699. cst_type:=cst_conststring;
  700. end;
  701. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  702. begin
  703. inherited create(stringconstn);
  704. len:=getlengthwidestring(w);
  705. initwidestring(pcompilerwidestring(value_str));
  706. copywidestring(w,pcompilerwidestring(value_str));
  707. lab_str:=nil;
  708. cst_type:=cst_unicodestring;
  709. end;
  710. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  711. begin
  712. inherited create(stringconstn);
  713. len:=l;
  714. value_str:=s;
  715. cst_type:=cst_conststring;
  716. lab_str:=nil;
  717. end;
  718. destructor tstringconstnode.destroy;
  719. begin
  720. if cst_type in [cst_widestring,cst_unicodestring] then
  721. donewidestring(pcompilerwidestring(value_str))
  722. else
  723. ansistringdispose(value_str,len);
  724. inherited destroy;
  725. end;
  726. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  727. var
  728. pw : pcompilerwidestring;
  729. i : longint;
  730. begin
  731. inherited ppuload(t,ppufile);
  732. cst_type:=tconststringtype(ppufile.getbyte);
  733. len:=ppufile.getlongint;
  734. if cst_type in [cst_widestring,cst_unicodestring] then
  735. begin
  736. initwidestring(pw);
  737. setlengthwidestring(pw,len);
  738. { don't use getdata, because the compilerwidechars may have to
  739. be byteswapped
  740. }
  741. {$if sizeof(tcompilerwidechar) = 2}
  742. for i:=0 to pw^.len-1 do
  743. pw^.data[i]:=ppufile.getword;
  744. {$elseif sizeof(tcompilerwidechar) = 4}
  745. for i:=0 to pw^.len-1 do
  746. pw^.data[i]:=cardinal(ppufile.getlongint);
  747. {$else}
  748. {$error Unsupported tcompilerwidechar size}
  749. {$endif}
  750. pcompilerwidestring(value_str):=pw
  751. end
  752. else
  753. begin
  754. getmem(value_str,len+1);
  755. ppufile.getdata(value_str^,len);
  756. value_str[len]:=#0;
  757. end;
  758. lab_str:=tasmlabel(ppufile.getasmsymbol);
  759. end;
  760. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  761. begin
  762. inherited ppuwrite(ppufile);
  763. ppufile.putbyte(byte(cst_type));
  764. ppufile.putlongint(len);
  765. if cst_type in [cst_widestring,cst_unicodestring] then
  766. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  767. else
  768. ppufile.putdata(value_str^,len);
  769. ppufile.putasmsymbol(lab_str);
  770. end;
  771. procedure tstringconstnode.buildderefimpl;
  772. begin
  773. inherited buildderefimpl;
  774. end;
  775. procedure tstringconstnode.derefimpl;
  776. begin
  777. inherited derefimpl;
  778. end;
  779. function tstringconstnode.dogetcopy : tnode;
  780. var
  781. n : tstringconstnode;
  782. begin
  783. n:=tstringconstnode(inherited dogetcopy);
  784. n.cst_type:=cst_type;
  785. n.len:=len;
  786. n.lab_str:=lab_str;
  787. if cst_type in [cst_widestring,cst_unicodestring] then
  788. begin
  789. initwidestring(pcompilerwidestring(n.value_str));
  790. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  791. end
  792. else
  793. n.value_str:=getpcharcopy;
  794. dogetcopy:=n;
  795. end;
  796. function tstringconstnode.pass_typecheck:tnode;
  797. var
  798. l : aint;
  799. begin
  800. result:=nil;
  801. case cst_type of
  802. cst_conststring :
  803. begin
  804. { handle and store as array[0..len-1] of char }
  805. if len>0 then
  806. l:=len-1
  807. else
  808. l:=0;
  809. resultdef:=tarraydef.create(0,l,s32inttype);
  810. tarraydef(resultdef).elementdef:=cansichartype;
  811. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  812. end;
  813. cst_shortstring :
  814. resultdef:=cshortstringtype;
  815. cst_ansistring :
  816. resultdef:=getansistringdef;
  817. cst_unicodestring :
  818. resultdef:=cunicodestringtype;
  819. cst_widestring :
  820. resultdef:=cwidestringtype;
  821. cst_longstring :
  822. resultdef:=clongstringtype;
  823. end;
  824. end;
  825. function tstringconstnode.pass_1 : tnode;
  826. begin
  827. result:=nil;
  828. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  829. begin
  830. if len=0 then
  831. expectloc:=LOC_CONSTANT
  832. else
  833. expectloc:=LOC_REGISTER
  834. end
  835. else
  836. expectloc:=LOC_CREFERENCE;
  837. if (cs_create_pic in current_settings.moduleswitches) and
  838. (expectloc <> LOC_CONSTANT) then
  839. include(current_procinfo.flags,pi_needs_got);
  840. end;
  841. function tstringconstnode.getpcharcopy : pchar;
  842. var
  843. pc : pchar;
  844. begin
  845. pc:=nil;
  846. getmem(pc,len+1);
  847. if pc=nil then
  848. Message(general_f_no_memory_left);
  849. move(value_str^,pc^,len+1);
  850. getpcharcopy:=pc;
  851. end;
  852. function tstringconstnode.docompare(p: tnode): boolean;
  853. begin
  854. docompare :=
  855. inherited docompare(p) and
  856. (len = tstringconstnode(p).len) and
  857. (lab_str = tstringconstnode(p).lab_str) and
  858. { This is enough as soon as labels are allocated, otherwise }
  859. { fall back to content compare. }
  860. (assigned(lab_str) or
  861. (cst_type = tstringconstnode(p).cst_type) and
  862. (fullcompare(tstringconstnode(p)) = 0))
  863. ;
  864. end;
  865. procedure tstringconstnode.changestringtype(def:tdef);
  866. const
  867. st2cst : array[tstringtype] of tconststringtype = (
  868. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  869. var
  870. pw : pcompilerwidestring;
  871. pc : pchar;
  872. cp1 : tstringencoding;
  873. cp2 : tstringencoding;
  874. l,l2 : longint;
  875. begin
  876. if def.typ<>stringdef then
  877. internalerror(200510011);
  878. { convert ascii 2 unicode }
  879. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  880. not(cst_type in [cst_widestring,cst_unicodestring]) then
  881. begin
  882. initwidestring(pw);
  883. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  884. ansistringdispose(value_str,len);
  885. pcompilerwidestring(value_str):=pw;
  886. end
  887. else
  888. { convert unicode 2 ascii }
  889. if (cst_type in [cst_widestring,cst_unicodestring]) and
  890. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  891. begin
  892. cp1:=tstringdef(def).encoding;
  893. if (cp1=CP_NONE) or (cp1=0) then
  894. cp1:=current_settings.sourcecodepage;
  895. if (cp1=CP_UTF8) then
  896. begin
  897. pw:=pcompilerwidestring(value_str);
  898. l2:=len;
  899. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  900. getmem(pc,l);
  901. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  902. len:=l-1;
  903. donewidestring(pw);
  904. value_str:=pc;
  905. end
  906. else
  907. begin
  908. pw:=pcompilerwidestring(value_str);
  909. getmem(pc,getlengthwidestring(pw)+1);
  910. unicode2ascii(pw,pc,cp1);
  911. donewidestring(pw);
  912. value_str:=pc;
  913. end;
  914. end
  915. else
  916. if (tstringdef(def).stringtype = st_ansistring) and
  917. not(cst_type in [cst_widestring,cst_unicodestring]) then
  918. begin
  919. cp1:=tstringdef(def).encoding;
  920. if cp1=0 then
  921. cp1:=current_settings.sourcecodepage;
  922. if (cst_type = cst_ansistring) then
  923. begin
  924. cp2:=tstringdef(resultdef).encoding;
  925. if cp2=0 then
  926. cp2:=current_settings.sourcecodepage;
  927. end
  928. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  929. cp2:=current_settings.sourcecodepage;
  930. { don't change string if codepages are equal or string length is 0 }
  931. if (cp1<>cp2) and (len>0) then
  932. begin
  933. if cpavailable(cp1) and cpavailable(cp2) then
  934. changecodepage(value_str,len,cp2,value_str,cp1)
  935. else if (cp1 <> CP_NONE) and (cp2 <> CP_NONE) then
  936. begin
  937. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  938. if (cp2=CP_UTF8) then
  939. begin
  940. if not cpavailable(cp1) then
  941. Message1(option_code_page_not_available,IntToStr(cp1));
  942. initwidestring(pw);
  943. setlengthwidestring(pw,len);
  944. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  945. if (l<>getlengthwidestring(pw)) then
  946. begin
  947. setlengthwidestring(pw,l);
  948. ReAllocMem(value_str,l);
  949. end;
  950. unicode2ascii(pw,value_str,cp1);
  951. donewidestring(pw);
  952. end
  953. else
  954. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  955. if (cp1=CP_UTF8) then
  956. begin
  957. if not cpavailable(cp2) then
  958. Message1(option_code_page_not_available,IntToStr(cp2));
  959. initwidestring(pw);
  960. setlengthwidestring(pw,len);
  961. ascii2unicode(value_str,len,cp2,pw);
  962. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  963. if l<>len then
  964. ReAllocMem(value_str,l);
  965. len:=l-1;
  966. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  967. donewidestring(pw);
  968. end
  969. else
  970. begin
  971. { output error message that encoding is not available for the compiler }
  972. if not cpavailable(cp1) then
  973. Message1(option_code_page_not_available,IntToStr(cp1));
  974. if not cpavailable(cp2) then
  975. Message1(option_code_page_not_available,IntToStr(cp2));
  976. end;
  977. end;
  978. end;
  979. end;
  980. cst_type:=st2cst[tstringdef(def).stringtype];
  981. resultdef:=def;
  982. end;
  983. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  984. begin
  985. if cst_type<>p.cst_type then
  986. InternalError(2009121701);
  987. if cst_type in [cst_widestring,cst_unicodestring] then
  988. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  989. else
  990. result:=compareansistrings(value_str,p.value_str,len,p.len);
  991. end;
  992. {*****************************************************************************
  993. TSETCONSTNODE
  994. *****************************************************************************}
  995. constructor tsetconstnode.create(s : pconstset;def:tdef);
  996. begin
  997. inherited create(setconstn,nil);
  998. typedef:=def;
  999. if assigned(s) then
  1000. begin
  1001. new(value_set);
  1002. value_set^:=s^;
  1003. end
  1004. else
  1005. value_set:=nil;
  1006. end;
  1007. destructor tsetconstnode.destroy;
  1008. begin
  1009. if assigned(value_set) then
  1010. dispose(value_set);
  1011. inherited destroy;
  1012. end;
  1013. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1014. begin
  1015. inherited ppuload(t,ppufile);
  1016. ppufile.getderef(typedefderef);
  1017. new(value_set);
  1018. ppufile.getnormalset(value_set^);
  1019. end;
  1020. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1021. begin
  1022. inherited ppuwrite(ppufile);
  1023. ppufile.putderef(typedefderef);
  1024. ppufile.putnormalset(value_set^);
  1025. end;
  1026. procedure tsetconstnode.buildderefimpl;
  1027. begin
  1028. inherited buildderefimpl;
  1029. typedefderef.build(typedef);
  1030. end;
  1031. procedure tsetconstnode.derefimpl;
  1032. begin
  1033. inherited derefimpl;
  1034. typedef:=tdef(typedefderef.resolve);
  1035. end;
  1036. procedure tsetconstnode.adjustforsetbase;
  1037. type
  1038. setbytes = array[0..31] of byte;
  1039. Psetbytes = ^setbytes;
  1040. var
  1041. i, diff: longint;
  1042. begin
  1043. { Internally, the compiler stores all sets with setbase 0, so we have }
  1044. { to convert the set to its actual format in case setbase<>0 when }
  1045. { writing it out }
  1046. if (tsetdef(resultdef).setbase<>0) then
  1047. begin
  1048. if (tsetdef(resultdef).setbase and 7)<>0 then
  1049. internalerror(2007091501);
  1050. diff:=tsetdef(resultdef).setbase div 8;
  1051. { This is endian-neutral in the new set format: in both cases, }
  1052. { the first byte contains the first elements of the set. }
  1053. { Since the compiler/base rtl cannot contain packed sets before }
  1054. { they work for big endian, it's no problem that the code below }
  1055. { is wrong for the old big endian set format (setbase cannot be }
  1056. { <>0 with non-packed sets). }
  1057. for i:=0 to tsetdef(resultdef).size-1 do
  1058. begin
  1059. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1060. Psetbytes(value_set)^[i+diff]:=0;
  1061. end;
  1062. end;
  1063. end;
  1064. function tsetconstnode.dogetcopy : tnode;
  1065. var
  1066. n : tsetconstnode;
  1067. begin
  1068. n:=tsetconstnode(inherited dogetcopy);
  1069. if assigned(value_set) then
  1070. begin
  1071. new(n.value_set);
  1072. n.value_set^:=value_set^
  1073. end
  1074. else
  1075. n.value_set:=nil;
  1076. n.typedef := typedef;
  1077. n.lab_set:=lab_set;
  1078. dogetcopy:=n;
  1079. end;
  1080. function tsetconstnode.pass_typecheck:tnode;
  1081. begin
  1082. result:=nil;
  1083. resultdef:=typedef;
  1084. end;
  1085. function tsetconstnode.pass_1 : tnode;
  1086. begin
  1087. result:=nil;
  1088. if is_smallset(resultdef) then
  1089. expectloc:=LOC_CONSTANT
  1090. else
  1091. expectloc:=LOC_CREFERENCE;
  1092. if (cs_create_pic in current_settings.moduleswitches) and
  1093. (expectloc <> LOC_CONSTANT) then
  1094. include(current_procinfo.flags,pi_needs_got);
  1095. end;
  1096. function tsetconstnode.docompare(p: tnode): boolean;
  1097. begin
  1098. docompare:=(inherited docompare(p)) and
  1099. (value_set^=Tsetconstnode(p).value_set^);
  1100. end;
  1101. {*****************************************************************************
  1102. TNILNODE
  1103. *****************************************************************************}
  1104. constructor tnilnode.create;
  1105. begin
  1106. inherited create(niln);
  1107. end;
  1108. function tnilnode.pass_typecheck:tnode;
  1109. begin
  1110. result:=nil;
  1111. resultdef:=voidpointertype;
  1112. end;
  1113. function tnilnode.pass_1 : tnode;
  1114. begin
  1115. result:=nil;
  1116. expectloc:=LOC_CONSTANT;
  1117. end;
  1118. {*****************************************************************************
  1119. TGUIDCONSTNODE
  1120. *****************************************************************************}
  1121. constructor tguidconstnode.create(const g:tguid);
  1122. begin
  1123. inherited create(guidconstn);
  1124. value:=g;
  1125. end;
  1126. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1127. begin
  1128. inherited ppuload(t,ppufile);
  1129. ppufile.getguid(value);
  1130. end;
  1131. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1132. begin
  1133. inherited ppuwrite(ppufile);
  1134. ppufile.putguid(value);
  1135. end;
  1136. function tguidconstnode.dogetcopy : tnode;
  1137. var
  1138. n : tguidconstnode;
  1139. begin
  1140. n:=tguidconstnode(inherited dogetcopy);
  1141. n.value:=value;
  1142. n.lab_set:=lab_set;
  1143. dogetcopy:=n;
  1144. end;
  1145. function tguidconstnode.pass_typecheck:tnode;
  1146. begin
  1147. result:=nil;
  1148. resultdef:=rec_tguid;
  1149. end;
  1150. function tguidconstnode.pass_1 : tnode;
  1151. begin
  1152. result:=nil;
  1153. expectloc:=LOC_CREFERENCE;
  1154. if (cs_create_pic in current_settings.moduleswitches) and
  1155. (tf_pic_uses_got in target_info.flags) then
  1156. include(current_procinfo.flags,pi_needs_got);
  1157. end;
  1158. function tguidconstnode.docompare(p: tnode): boolean;
  1159. begin
  1160. docompare :=
  1161. inherited docompare(p) and
  1162. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1163. end;
  1164. end.