ncon.pas 40 KB

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