ncon.pas 42 KB

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