ncon.pas 40 KB

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