ncon.pas 42 KB

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