ptconst.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Reads typed constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptconst;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses symtype,symsym;
  22. { this procedure reads typed constants }
  23. { sym is only needed for ansi strings }
  24. { the assembler label is in the middle (PM) }
  25. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  26. implementation
  27. uses
  28. strings,
  29. globtype,systems,tokens,verbose,
  30. cutils,globals,widestr,scanner,
  31. symconst,symbase,symdef,symtable,
  32. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  33. { pass 1 }
  34. node,htypechk,
  35. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  36. { parser specific stuff }
  37. pbase,pexpr,
  38. { codegen }
  39. cpuinfo,cgbase
  40. ;
  41. {$ifdef fpc}
  42. {$maxfpuregisters 0}
  43. {$endif fpc}
  44. { this procedure reads typed constants }
  45. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  46. type
  47. setbytes = array[0..31] of byte;
  48. Psetbytes = ^setbytes;
  49. var
  50. len,base : longint;
  51. p,hp : tnode;
  52. i,j,l,
  53. varalign : longint;
  54. offset,
  55. strlength : aint;
  56. curconstsegment : TAAsmoutput;
  57. ll : tasmlabel;
  58. s,sorg : string;
  59. c : char;
  60. ca : pchar;
  61. tmpguid : tguid;
  62. aktpos : longint;
  63. obj : tobjectdef;
  64. recsym,
  65. srsym : tsym;
  66. symt : tsymtable;
  67. value : bestreal;
  68. intvalue : tconstexprint;
  69. strval : pchar;
  70. pw : pcompilerwidestring;
  71. error : boolean;
  72. old_block_type : tblock_type;
  73. procedure check_range(def:torddef);
  74. begin
  75. if ((tordconstnode(p).value>def.high) or
  76. (tordconstnode(p).value<def.low)) then
  77. begin
  78. if (cs_check_range in aktlocalswitches) then
  79. Message(parser_e_range_check_error)
  80. else
  81. Message(parser_w_range_check_error);
  82. end;
  83. end;
  84. begin
  85. old_block_type:=block_type;
  86. block_type:=bt_const;
  87. if writable then
  88. curconstsegment:=datasegment
  89. else
  90. curconstsegment:=consts;
  91. case t.def.deftype of
  92. orddef:
  93. begin
  94. p:=comp_expr(true);
  95. case torddef(t.def).typ of
  96. bool8bit :
  97. begin
  98. if is_constboolnode(p) then
  99. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  100. else
  101. Message(parser_e_illegal_expression);
  102. end;
  103. bool16bit :
  104. begin
  105. if is_constboolnode(p) then
  106. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  107. else
  108. Message(parser_e_illegal_expression);
  109. end;
  110. bool32bit :
  111. begin
  112. if is_constboolnode(p) then
  113. curconstSegment.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)))
  114. else
  115. Message(parser_e_illegal_expression);
  116. end;
  117. uchar :
  118. begin
  119. if is_constcharnode(p) then
  120. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  121. else
  122. Message(parser_e_illegal_expression);
  123. end;
  124. uwidechar :
  125. begin
  126. if is_constcharnode(p) then
  127. inserttypeconv(p,cwidechartype);
  128. if is_constwidecharnode(p) then
  129. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  130. else
  131. Message(parser_e_illegal_expression);
  132. end;
  133. s8bit,
  134. u8bit :
  135. begin
  136. if is_constintnode(p) then
  137. begin
  138. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)));
  139. check_range(torddef(t.def));
  140. end
  141. else
  142. Message(parser_e_illegal_expression);
  143. end;
  144. u16bit,
  145. s16bit :
  146. begin
  147. if is_constintnode(p) then
  148. begin
  149. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)));
  150. check_range(torddef(t.def));
  151. end
  152. else
  153. Message(parser_e_illegal_expression);
  154. end;
  155. s32bit,
  156. u32bit :
  157. begin
  158. if is_constintnode(p) then
  159. begin
  160. curconstSegment.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)));
  161. if torddef(t.def).typ<>u32bit then
  162. check_range(torddef(t.def));
  163. end
  164. else
  165. Message(parser_e_illegal_expression);
  166. end;
  167. s64bit,
  168. u64bit,
  169. scurrency:
  170. begin
  171. if is_constintnode(p) then
  172. intvalue := tordconstnode(p).value
  173. else if is_constrealnode(p) and
  174. (torddef(t.def).typ=scurrency)
  175. { allow bootstrapping }
  176. {$ifdef VER1_0}
  177. { I get IE 10 here. I guess it's no problem if a 1.0 bootstrapped
  178. compiler doesn't display the error here.
  179. and (trealconstnode(p).value_real*10000 >= real(low(int64))) and
  180. (trealconstnode(p).value_real*10000 <= real(high(int64)))}
  181. {$endif VER1_0}
  182. then
  183. begin
  184. {$ifdef VER1_0}
  185. { only trunc is 64 bit in 1.0.x so use trunc here to allow bootstrapping }
  186. intvalue:=trunc(trealconstnode(p).value_real*10000);
  187. {$else VER1_0}
  188. intvalue:=round(trealconstnode(p).value_real*10000);
  189. {$endif VER1_0}
  190. end
  191. else
  192. begin
  193. intvalue:=0;
  194. Message(parser_e_illegal_expression);
  195. end;
  196. curconstSegment.concat(Tai_const.Create_64bit(intvalue));
  197. end;
  198. else
  199. internalerror(3799);
  200. end;
  201. p.free;
  202. end;
  203. floatdef:
  204. begin
  205. p:=comp_expr(true);
  206. if is_constrealnode(p) then
  207. value:=trealconstnode(p).value_real
  208. else if is_constintnode(p) then
  209. value:=tordconstnode(p).value
  210. else
  211. Message(parser_e_illegal_expression);
  212. case tfloatdef(t.def).typ of
  213. s32real :
  214. curconstSegment.concat(Tai_real_32bit.Create(ts32real(value)));
  215. s64real :
  216. {$ifdef ARM}
  217. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  218. curconstSegment.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  219. else
  220. {$endif ARM}
  221. curconstSegment.concat(Tai_real_64bit.Create(ts64real(value)));
  222. s80real :
  223. curconstSegment.concat(Tai_real_80bit.Create(value));
  224. {$ifdef ver1_0}
  225. s64comp :
  226. curconstSegment.concat(Tai_comp_64bit.Create(value));
  227. s64currency:
  228. curconstSegment.concat(Tai_comp_64bit.Create(value*10000));
  229. {$else ver1_0}
  230. { the round is necessary for native compilers where comp isn't a float }
  231. s64comp :
  232. curconstSegment.concat(Tai_comp_64bit.Create(round(value)));
  233. s64currency:
  234. curconstSegment.concat(Tai_comp_64bit.Create(round(value*10000)));
  235. {$endif ver1_0}
  236. s128real:
  237. curconstSegment.concat(Tai_real_128bit.Create(value));
  238. else
  239. internalerror(18);
  240. end;
  241. p.free;
  242. end;
  243. classrefdef:
  244. begin
  245. p:=comp_expr(true);
  246. case p.nodetype of
  247. loadvmtaddrn:
  248. with Tclassrefdef(p.resulttype.def) do
  249. begin
  250. if not Tobjectdef(pointertype.def).is_related(Tobjectdef(pointertype.def)) then
  251. message(parser_e_illegal_expression);
  252. curconstSegment.concat(Tai_const.Create_sym(objectlibrary.newasmsymbol(
  253. Tobjectdef(pointertype.def).vmt_mangledname,AB_EXTERNAL,AT_DATA)));
  254. end;
  255. niln:
  256. curconstSegment.concat(Tai_const.Create_sym(nil));
  257. else Message(parser_e_illegal_expression);
  258. end;
  259. p.free;
  260. end;
  261. pointerdef:
  262. begin
  263. p:=comp_expr(true);
  264. if (p.nodetype=typeconvn) then
  265. with Ttypeconvnode(p) do
  266. if (left.nodetype in [addrn,niln]) and equal_defs(t.def,p.resulttype.def) then
  267. begin
  268. hp:=left;
  269. left:=nil;
  270. p.free;
  271. p:=hp;
  272. end;
  273. { allows horrible ofs(typeof(TButton)^) code !! }
  274. if (p.nodetype=addrn) then
  275. with Taddrnode(p) do
  276. if left.nodetype=derefn then
  277. begin
  278. hp:=tderefnode(left).left;
  279. tderefnode(left).left:=nil;
  280. p.free;
  281. p:=hp;
  282. end;
  283. { const pointer ? }
  284. if (p.nodetype = pointerconstn) then
  285. begin
  286. if sizeof(TConstPtrUInt)=8 then
  287. curconstsegment.concat(Tai_const.Create_64bit(TConstPtrUInt(tpointerconstnode(p).value)))
  288. else
  289. if sizeof(TConstPtrUInt)=4 then
  290. curconstsegment.concat(Tai_const.Create_32bit(TConstPtrUInt(tpointerconstnode(p).value)))
  291. else
  292. internalerror(200404122);
  293. end
  294. { nil pointer ? }
  295. else if p.nodetype=niln then
  296. curconstSegment.concat(Tai_const.Create_sym(nil))
  297. { maybe pchar ? }
  298. else
  299. if is_char(tpointerdef(t.def).pointertype.def) and
  300. (p.nodetype<>addrn) then
  301. begin
  302. objectlibrary.getdatalabel(ll);
  303. curconstSegment.concat(Tai_const.Create_sym(ll));
  304. if p.nodetype=stringconstn then
  305. varalign:=tstringconstnode(p).len
  306. else
  307. varalign:=0;
  308. varalign:=const_align(varalign);
  309. Consts.concat(Tai_align.Create(varalign));
  310. Consts.concat(Tai_label.Create(ll));
  311. if p.nodetype=stringconstn then
  312. begin
  313. len:=tstringconstnode(p).len;
  314. { For tp7 the maximum lentgh can be 255 }
  315. if (m_tp7 in aktmodeswitches) and
  316. (len>255) then
  317. len:=255;
  318. getmem(ca,len+2);
  319. move(tstringconstnode(p).value_str^,ca^,len+1);
  320. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  321. end
  322. else
  323. if is_constcharnode(p) then
  324. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  325. else
  326. Message(parser_e_illegal_expression);
  327. end
  328. { maybe pwidechar ? }
  329. else
  330. if is_widechar(tpointerdef(t.def).pointertype.def) and
  331. (p.nodetype<>addrn) then
  332. begin
  333. objectlibrary.getdatalabel(ll);
  334. curconstSegment.concat(Tai_const.Create_sym(ll));
  335. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  336. Consts.concat(Tai_label.Create(ll));
  337. if (p.nodetype in [stringconstn,ordconstn]) then
  338. begin
  339. { convert to widestring stringconstn }
  340. inserttypeconv(p,cwidestringtype);
  341. if (p.nodetype=stringconstn) and
  342. (tstringconstnode(p).st_type=st_widestring) then
  343. begin
  344. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  345. for i:=0 to tstringconstnode(p).len-1 do
  346. Consts.concat(Tai_const.Create_16bit(pw^.data[i]));
  347. { ending #0 }
  348. Consts.concat(Tai_const.Create_16bit(0))
  349. end;
  350. end
  351. else
  352. Message(parser_e_illegal_expression);
  353. end
  354. else
  355. if (p.nodetype=addrn) or
  356. is_procvar_load(p) then
  357. begin
  358. { insert typeconv }
  359. inserttypeconv(p,t);
  360. hp:=p;
  361. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  362. hp:=tunarynode(hp).left;
  363. if (hp.nodetype=loadn) then
  364. begin
  365. hp:=p;
  366. offset:=0;
  367. while assigned(hp) and (hp.nodetype<>loadn) do
  368. begin
  369. case hp.nodetype of
  370. vecn :
  371. begin
  372. case tvecnode(hp).left.resulttype.def.deftype of
  373. stringdef :
  374. begin
  375. { this seems OK for shortstring and ansistrings PM }
  376. { it is wrong for widestrings !! }
  377. len:=1;
  378. base:=0;
  379. end;
  380. arraydef :
  381. begin
  382. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  383. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  384. end
  385. else
  386. Message(parser_e_illegal_expression);
  387. end;
  388. if is_constintnode(tvecnode(hp).right) then
  389. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  390. else
  391. Message(parser_e_illegal_expression);
  392. end;
  393. subscriptn :
  394. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  395. typeconvn :
  396. begin
  397. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  398. Message(parser_e_illegal_expression);
  399. end;
  400. addrn :
  401. ;
  402. else
  403. Message(parser_e_illegal_expression);
  404. end;
  405. hp:=tunarynode(hp).left;
  406. end;
  407. srsym:=tloadnode(hp).symtableentry;
  408. case srsym.typ of
  409. procsym :
  410. begin
  411. if Tprocsym(srsym).procdef_count>1 then
  412. Message(parser_e_no_overloaded_procvars);
  413. if po_abstractmethod in tprocsym(srsym).first_procdef.procoptions then
  414. Message(type_e_cant_take_address_of_abstract_method)
  415. else
  416. curconstSegment.concat(Tai_const.Createname(tprocsym(srsym).first_procdef.mangledname,AT_FUNCTION,offset));
  417. end;
  418. globalvarsym :
  419. curconstSegment.concat(Tai_const.Createname(tglobalvarsym(srsym).mangledname,AT_DATA,offset));
  420. typedconstsym :
  421. curconstSegment.concat(Tai_const.Createname(ttypedconstsym(srsym).mangledname,AT_DATA,offset));
  422. labelsym :
  423. curconstSegment.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,AT_FUNCTION,offset));
  424. constsym :
  425. if tconstsym(srsym).consttyp=constresourcestring then
  426. curconstSegment.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',tconstsym(srsym).owner,''),AT_DATA,tconstsym(srsym).resstrindex*(4+sizeof(aint)*3)+4+sizeof(aint)))
  427. else
  428. Message(type_e_variable_id_expected);
  429. else
  430. Message(type_e_variable_id_expected);
  431. end;
  432. end
  433. else
  434. Message(parser_e_illegal_expression);
  435. end
  436. else
  437. { allow typeof(Object type)}
  438. if (p.nodetype=inlinen) and
  439. (tinlinenode(p).inlinenumber=in_typeof_x) then
  440. begin
  441. if (tinlinenode(p).left.nodetype=typen) then
  442. begin
  443. curconstSegment.concat(Tai_const.createname(
  444. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname,AT_DATA,0));
  445. end
  446. else
  447. Message(parser_e_illegal_expression);
  448. end
  449. else
  450. Message(parser_e_illegal_expression);
  451. p.free;
  452. end;
  453. setdef:
  454. begin
  455. p:=comp_expr(true);
  456. if p.nodetype=setconstn then
  457. begin
  458. { be sure to convert to the correct result, else
  459. it can generate smallset data instead of normalset (PFV) }
  460. inserttypeconv(p,t);
  461. { we only allow const sets }
  462. if assigned(tsetconstnode(p).left) then
  463. Message(parser_e_illegal_expression)
  464. else
  465. begin
  466. { this writing is endian independant }
  467. { untrue - because they are considered }
  468. { arrays of 32-bit values CEC }
  469. if source_info.endian = target_info.endian then
  470. begin
  471. for l:=0 to p.resulttype.def.size-1 do
  472. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[l]));
  473. end
  474. else
  475. begin
  476. { store as longint values in swaped format }
  477. j:=0;
  478. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  479. begin
  480. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  481. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  482. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  483. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  484. Inc(j,4);
  485. end;
  486. end;
  487. end;
  488. end
  489. else
  490. Message(parser_e_illegal_expression);
  491. p.free;
  492. end;
  493. enumdef:
  494. begin
  495. p:=comp_expr(true);
  496. if p.nodetype=ordconstn then
  497. begin
  498. if equal_defs(p.resulttype.def,t.def) or
  499. is_subequal(p.resulttype.def,t.def) then
  500. begin
  501. case longint(p.resulttype.def.size) of
  502. 1 : curconstSegment.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  503. 2 : curconstSegment.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  504. 4 : curconstSegment.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  505. end;
  506. end
  507. else
  508. IncompatibleTypes(p.resulttype.def,t.def);
  509. end
  510. else
  511. Message(parser_e_illegal_expression);
  512. p.free;
  513. end;
  514. stringdef:
  515. begin
  516. p:=comp_expr(true);
  517. { load strval and strlength of the constant tree }
  518. if (p.nodetype=stringconstn) or is_widestring(t.def) then
  519. begin
  520. { convert to the expected string type so that
  521. for widestrings strval is a pcompilerwidestring }
  522. inserttypeconv(p,t);
  523. strlength:=tstringconstnode(p).len;
  524. strval:=tstringconstnode(p).value_str;
  525. end
  526. else if is_constcharnode(p) then
  527. begin
  528. { strval:=pchar(@tordconstnode(p).value);
  529. THIS FAIL on BIG_ENDIAN MACHINES PM }
  530. c:=chr(tordconstnode(p).value and $ff);
  531. strval:=@c;
  532. strlength:=1
  533. end
  534. else if is_constresourcestringnode(p) then
  535. begin
  536. strval:=pchar(tconstsym(tloadnode(p).symtableentry).value.valueptr);
  537. strlength:=tconstsym(tloadnode(p).symtableentry).value.len;
  538. end
  539. else
  540. begin
  541. Message(parser_e_illegal_expression);
  542. strlength:=-1;
  543. end;
  544. if strlength>=0 then
  545. begin
  546. case tstringdef(t.def).string_typ of
  547. st_shortstring:
  548. begin
  549. if strlength>=t.def.size then
  550. begin
  551. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  552. strlength:=t.def.size-1;
  553. end;
  554. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  555. { this can also handle longer strings }
  556. getmem(ca,strlength+1);
  557. move(strval^,ca^,strlength);
  558. ca[strlength]:=#0;
  559. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  560. { fillup with spaces if size is shorter }
  561. if t.def.size>strlength then
  562. begin
  563. getmem(ca,t.def.size-strlength);
  564. { def.size contains also the leading length, so we }
  565. { we have to subtract one }
  566. fillchar(ca[0],t.def.size-strlength-1,' ');
  567. ca[t.def.size-strlength-1]:=#0;
  568. { this can also handle longer strings }
  569. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  570. end;
  571. end;
  572. {$ifdef ansistrings_bits}
  573. st_ansistring16:
  574. begin
  575. { an empty ansi string is nil! }
  576. if (strlength=0) then
  577. curconstSegment.concat(Tai_const.Create_ptr(0))
  578. else
  579. begin
  580. objectlibrary.getdatalabel(ll);
  581. curconstSegment.concat(Tai_const_symbol.Create(ll));
  582. { the actual structure starts at -12 from start label - CEC }
  583. Consts.concat(tai_align.create(const_align(pointer_size)));
  584. { first write the maximum size }
  585. Consts.concat(Tai_const.Create_16bit(strlength));
  586. { second write the real length }
  587. Consts.concat(Tai_const.Create_16bit(strlength));
  588. { redondent with maxlength but who knows ... (PM) }
  589. { third write use count (set to -1 for safety ) }
  590. Consts.concat(Tai_const.Create_16bit(-1));
  591. Consts.concat(Tai_label.Create(ll));
  592. getmem(ca,strlength+2);
  593. move(strval^,ca^,strlength);
  594. { The terminating #0 to be stored in the .data section (JM) }
  595. ca[strlength]:=#0;
  596. { End of the PChar. The memory has to be allocated because in }
  597. { tai_string.done, there is a freemem(len+1) (JM) }
  598. ca[strlength+1]:=#0;
  599. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  600. end;
  601. end;
  602. {$endif}
  603. {$ifdef ansistring_bits}st_ansistring32{$else}st_ansistring{$endif}:
  604. begin
  605. { an empty ansi string is nil! }
  606. if (strlength=0) then
  607. curconstSegment.concat(Tai_const.Create_sym(nil))
  608. else
  609. begin
  610. objectlibrary.getdatalabel(ll);
  611. curconstSegment.concat(Tai_const.Create_sym(ll));
  612. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  613. Consts.concat(Tai_const.Create_aint(-1));
  614. Consts.concat(Tai_const.Create_aint(strlength));
  615. Consts.concat(Tai_label.Create(ll));
  616. getmem(ca,strlength+2);
  617. move(strval^,ca^,strlength);
  618. { The terminating #0 to be stored in the .data section (JM) }
  619. ca[strlength]:=#0;
  620. { End of the PChar. The memory has to be allocated because in }
  621. { tai_string.done, there is a freemem(len+1) (JM) }
  622. ca[strlength+1]:=#0;
  623. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  624. end;
  625. end;
  626. {$ifdef ansistring_bits}
  627. st_ansistring64:
  628. begin
  629. { an empty ansi string is nil! }
  630. if (strlength=0) then
  631. curconstSegment.concat(Tai_const.Create_ptr(0))
  632. else
  633. begin
  634. objectlibrary.getdatalabel(ll);
  635. curconstSegment.concat(Tai_const_symbol.Create(ll));
  636. { the actual structure starts at -12 from start label - CEC }
  637. Consts.concat(tai_align.create(const_align(pointer_size)));
  638. { first write the maximum size }
  639. Consts.concat(Tai_const.Create_64bit(strlength));
  640. { second write the real length }
  641. Consts.concat(Tai_const.Create_64bit(strlength));
  642. { redondent with maxlength but who knows ... (PM) }
  643. { third write use count (set to -1 for safety ) }
  644. Consts.concat(Tai_const.Create_64bit(-1));
  645. Consts.concat(Tai_label.Create(ll));
  646. getmem(ca,strlength+2);
  647. move(strval^,ca^,strlength);
  648. { The terminating #0 to be stored in the .data section (JM) }
  649. ca[strlength]:=#0;
  650. { End of the PChar. The memory has to be allocated because in }
  651. { tai_string.done, there is a freemem(len+1) (JM) }
  652. ca[strlength+1]:=#0;
  653. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  654. end;
  655. end;
  656. {$endif}
  657. st_widestring:
  658. begin
  659. { an empty ansi string is nil! }
  660. if (strlength=0) then
  661. curconstSegment.concat(Tai_const.Create_sym(nil))
  662. else
  663. begin
  664. objectlibrary.getdatalabel(ll);
  665. curconstSegment.concat(Tai_const.Create_sym(ll));
  666. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  667. Consts.concat(Tai_const.Create_aint(-1));
  668. Consts.concat(Tai_const.Create_aint(strlength));
  669. Consts.concat(Tai_label.Create(ll));
  670. for i:=0 to strlength-1 do
  671. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  672. { ending #0 }
  673. Consts.concat(Tai_const.Create_16bit(0))
  674. end;
  675. end;
  676. st_longstring:
  677. begin
  678. internalerror(200107081);
  679. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  680. curconstSegment.concat(Tai_const.Create_8bit(0));
  681. getmem(ca,strlength+1);
  682. move(strval^,ca^,strlength);
  683. ca[strlength]:=#0;
  684. generate_pascii(consts,ca,strlength);
  685. curconstSegment.concat(Tai_const.Create_8bit(0));}
  686. end;
  687. end;
  688. end;
  689. p.free;
  690. end;
  691. arraydef:
  692. begin
  693. { dynamic array nil }
  694. if is_dynamic_array(t.def) then
  695. begin
  696. { Only allow nil initialization }
  697. consume(_NIL);
  698. curconstSegment.concat(Tai_const.Create_sym(nil));
  699. end
  700. else
  701. if try_to_consume(_LKLAMMER) then
  702. begin
  703. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  704. begin
  705. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  706. consume(_COMMA);
  707. end;
  708. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  709. consume(_RKLAMMER);
  710. end
  711. else
  712. { if array of char then we allow also a string }
  713. if is_char(tarraydef(t.def).elementtype.def) then
  714. begin
  715. p:=comp_expr(true);
  716. if p.nodetype=stringconstn then
  717. begin
  718. len:=tstringconstnode(p).len;
  719. { For tp7 the maximum lentgh can be 255 }
  720. if (m_tp7 in aktmodeswitches) and
  721. (len>255) then
  722. len:=255;
  723. ca:=tstringconstnode(p).value_str;
  724. end
  725. else
  726. if is_constcharnode(p) then
  727. begin
  728. c:=chr(tordconstnode(p).value and $ff);
  729. ca:=@c;
  730. len:=1;
  731. end
  732. else
  733. begin
  734. Message(parser_e_illegal_expression);
  735. len:=0;
  736. end;
  737. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  738. Message(parser_e_string_larger_array);
  739. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  740. begin
  741. if i+1-tarraydef(t.def).lowrange<=len then
  742. begin
  743. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  744. inc(ca);
  745. end
  746. else
  747. {Fill the remaining positions with #0.}
  748. curconstSegment.concat(Tai_const.Create_8bit(0));
  749. end;
  750. p.free;
  751. end
  752. else
  753. begin
  754. { we want the ( }
  755. consume(_LKLAMMER);
  756. end;
  757. end;
  758. procvardef:
  759. begin
  760. { Procvars and pointers are no longer compatible. }
  761. { under tp: =nil or =var under fpc: =nil or =@var }
  762. if token=_NIL then
  763. begin
  764. curconstSegment.concat(Tai_const.Create_sym(nil));
  765. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  766. curconstSegment.concat(Tai_const.Create_sym(nil));
  767. consume(_NIL);
  768. exit;
  769. end;
  770. { you can't assign a value other than NIL to a typed constant }
  771. { which is a "procedure of object", because this also requires }
  772. { address of an object/class instance, which is not known at }
  773. { compile time (JM) }
  774. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  775. Message(parser_e_no_procvarobj_const);
  776. { parse the rest too, so we can continue with error checking }
  777. getprocvardef:=tprocvardef(t.def);
  778. p:=comp_expr(true);
  779. getprocvardef:=nil;
  780. if codegenerror then
  781. begin
  782. p.free;
  783. exit;
  784. end;
  785. { let type conversion check everything needed }
  786. inserttypeconv(p,t);
  787. if codegenerror then
  788. begin
  789. p.free;
  790. exit;
  791. end;
  792. { remove typeconvs, that will normally insert a lea
  793. instruction which is not necessary for us }
  794. while p.nodetype=typeconvn do
  795. begin
  796. hp:=ttypeconvnode(p).left;
  797. ttypeconvnode(p).left:=nil;
  798. p.free;
  799. p:=hp;
  800. end;
  801. { remove addrn which we also don't need here }
  802. if p.nodetype=addrn then
  803. begin
  804. hp:=taddrnode(p).left;
  805. taddrnode(p).left:=nil;
  806. p.free;
  807. p:=hp;
  808. end;
  809. { we now need to have a loadn with a procsym }
  810. if (p.nodetype=loadn) and
  811. (tloadnode(p).symtableentry.typ=procsym) then
  812. begin
  813. curconstSegment.concat(Tai_const.createname(
  814. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,AT_FUNCTION,0));
  815. end
  816. else
  817. Message(parser_e_illegal_expression);
  818. p.free;
  819. end;
  820. { reads a typed constant record }
  821. recorddef:
  822. begin
  823. { KAZ }
  824. if (trecorddef(t.def)=rec_tguid) and
  825. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  826. begin
  827. p:=comp_expr(true);
  828. inserttypeconv(p,cshortstringtype);
  829. if p.nodetype=stringconstn then
  830. begin
  831. s:=strpas(tstringconstnode(p).value_str);
  832. p.free;
  833. if string2guid(s,tmpguid) then
  834. begin
  835. curconstSegment.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  836. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  837. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  838. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  839. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  840. end
  841. else
  842. Message(parser_e_improper_guid_syntax);
  843. end
  844. else
  845. begin
  846. p.free;
  847. Message(parser_e_illegal_expression);
  848. exit;
  849. end;
  850. end
  851. else
  852. begin
  853. consume(_LKLAMMER);
  854. sorg:='';
  855. aktpos:=0;
  856. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  857. recsym := nil;
  858. while token<>_RKLAMMER do
  859. begin
  860. s:=pattern;
  861. sorg:=orgpattern;
  862. consume(_ID);
  863. consume(_COLON);
  864. error := false;
  865. recsym := tsym(trecorddef(t.def).symtable.search(s));
  866. if not assigned(recsym) then
  867. begin
  868. Message1(sym_e_illegal_field,sorg);
  869. error := true;
  870. end;
  871. if (not error) and
  872. (not assigned(srsym) or
  873. (s <> srsym.name)) then
  874. { possible variant record (JM) }
  875. begin
  876. { All parts of a variant start at the same offset }
  877. { Also allow jumping from one variant part to another, }
  878. { as long as the offsets match }
  879. if (assigned(srsym) and
  880. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  881. { srsym is not assigned after parsing w2 in the }
  882. { typed const in the next example: }
  883. { type tr = record case byte of }
  884. { 1: (l1,l2: dword); }
  885. { 2: (w1,w2: word); }
  886. { end; }
  887. { const r: tr = (w1:1;w2:1;l2:5); }
  888. (tfieldvarsym(recsym).fieldoffset = aktpos) then
  889. srsym := recsym
  890. { going backwards isn't allowed in any mode }
  891. else if (tfieldvarsym(recsym).fieldoffset<aktpos) then
  892. begin
  893. Message(parser_e_invalid_record_const);
  894. error := true;
  895. end
  896. { Delphi allows you to skip fields }
  897. else if (m_delphi in aktmodeswitches) then
  898. begin
  899. Message1(parser_w_skipped_fields_before,sorg);
  900. srsym := recsym;
  901. end
  902. { FPC and TP don't }
  903. else
  904. begin
  905. Message1(parser_e_skipped_fields_before,sorg);
  906. error := true;
  907. end;
  908. end;
  909. if error then
  910. consume_all_until(_SEMICOLON)
  911. else
  912. begin
  913. { if needed fill (alignment) }
  914. if tfieldvarsym(srsym).fieldoffset>aktpos then
  915. for i:=1 to tfieldvarsym(srsym).fieldoffset-aktpos do
  916. curconstSegment.concat(Tai_const.Create_8bit(0));
  917. { new position }
  918. aktpos:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vartype.def.size;
  919. { read the data }
  920. readtypedconst(tfieldvarsym(srsym).vartype,nil,writable);
  921. { keep previous field for checking whether whole }
  922. { record was initialized (JM) }
  923. recsym := srsym;
  924. { goto next field }
  925. srsym := tsym(srsym.indexnext);
  926. if token=_SEMICOLON then
  927. consume(_SEMICOLON)
  928. else break;
  929. end;
  930. end;
  931. { are there any fields left? }
  932. if assigned(srsym) and
  933. { don't complain if there only come other variant parts }
  934. { after the last initialized field }
  935. ((recsym=nil) or
  936. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)) then
  937. Message1(parser_w_skipped_fields_after,sorg);
  938. for i:=1 to t.def.size-aktpos do
  939. curconstSegment.concat(Tai_const.Create_8bit(0));
  940. consume(_RKLAMMER);
  941. end;
  942. end;
  943. { reads a typed object }
  944. objectdef:
  945. begin
  946. if is_class_or_interface(t.def) then
  947. begin
  948. p:=comp_expr(true);
  949. if p.nodetype<>niln then
  950. begin
  951. Message(parser_e_type_const_not_possible);
  952. consume_all_until(_RKLAMMER);
  953. end
  954. else
  955. begin
  956. curconstSegment.concat(Tai_const.Create_sym(nil));
  957. end;
  958. p.free;
  959. end
  960. { for objects we allow it only if it doesn't contain a vmt }
  961. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  962. (m_fpc in aktmodeswitches) then
  963. Message(parser_e_type_const_not_possible)
  964. else
  965. begin
  966. consume(_LKLAMMER);
  967. aktpos:=0;
  968. while token<>_RKLAMMER do
  969. begin
  970. s:=pattern;
  971. sorg:=orgpattern;
  972. consume(_ID);
  973. consume(_COLON);
  974. srsym:=nil;
  975. obj:=tobjectdef(t.def);
  976. symt:=obj.symtable;
  977. while (srsym=nil) and assigned(symt) do
  978. begin
  979. srsym:=tsym(symt.search(s));
  980. if assigned(obj) then
  981. obj:=obj.childof;
  982. if assigned(obj) then
  983. symt:=obj.symtable
  984. else
  985. symt:=nil;
  986. end;
  987. if srsym=nil then
  988. begin
  989. Message1(sym_e_id_not_found,sorg);
  990. consume_all_until(_SEMICOLON);
  991. end
  992. else
  993. with tfieldvarsym(srsym) do
  994. begin
  995. { check position }
  996. if fieldoffset<aktpos then
  997. message(parser_e_invalid_record_const);
  998. { check in VMT needs to be added for TP mode }
  999. with Tobjectdef(t.def) do
  1000. if not(m_fpc in aktmodeswitches) and
  1001. (oo_has_vmt in objectoptions) and
  1002. (vmt_offset<fieldoffset) then
  1003. begin
  1004. for i:=1 to vmt_offset-aktpos do
  1005. curconstsegment.concat(tai_const.create_8bit(0));
  1006. curconstsegment.concat(tai_const.createname(vmt_mangledname,AT_DATA,0));
  1007. { this is more general }
  1008. aktpos:=vmt_offset + sizeof(aint);
  1009. end;
  1010. { if needed fill }
  1011. if fieldoffset>aktpos then
  1012. for i:=1 to fieldoffset-aktpos do
  1013. curconstSegment.concat(Tai_const.Create_8bit(0));
  1014. { new position }
  1015. aktpos:=fieldoffset+vartype.def.size;
  1016. { read the data }
  1017. readtypedconst(vartype,nil,writable);
  1018. if token=_SEMICOLON then
  1019. consume(_SEMICOLON)
  1020. else break;
  1021. end;
  1022. end;
  1023. if not(m_fpc in aktmodeswitches) and
  1024. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  1025. (tobjectdef(t.def).vmt_offset>=aktpos) then
  1026. begin
  1027. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  1028. curconstsegment.concat(tai_const.create_8bit(0));
  1029. curconstsegment.concat(tai_const.createname(tobjectdef(t.def).vmt_mangledname,AT_DATA,0));
  1030. { this is more general }
  1031. aktpos:=tobjectdef(t.def).vmt_offset + sizeof(aint);
  1032. end;
  1033. for i:=1 to t.def.size-aktpos do
  1034. curconstSegment.concat(Tai_const.Create_8bit(0));
  1035. consume(_RKLAMMER);
  1036. end;
  1037. end;
  1038. errordef:
  1039. begin
  1040. { try to consume something useful }
  1041. if token=_LKLAMMER then
  1042. consume_all_until(_RKLAMMER)
  1043. else
  1044. consume_all_until(_SEMICOLON);
  1045. end;
  1046. else Message(parser_e_type_const_not_possible);
  1047. end;
  1048. block_type:=old_block_type;
  1049. end;
  1050. {$ifdef fpc}
  1051. {$maxfpuregisters default}
  1052. {$endif fpc}
  1053. end.
  1054. {
  1055. $Log$
  1056. Revision 1.101 2005-02-02 19:42:19 florian
  1057. + allow to take the address of a resourcestring in typed constants
  1058. Revision 1.100 2005/01/24 21:39:03 peter
  1059. * remove multiple typeconvs for procvar
  1060. Revision 1.99 2005/01/09 15:05:29 peter
  1061. * fix interface vtbl optimization
  1062. * replace ugly pointer construct of ioffset()
  1063. Revision 1.98 2005/01/08 14:05:31 florian
  1064. * typed dyn. array constants can be only nil pointer
  1065. Revision 1.97 2004/12/05 12:28:11 peter
  1066. * procvar handling for tp procvar mode fixed
  1067. * proc to procvar moved from addrnode to typeconvnode
  1068. * inlininginfo is now allocated only for inline routines that
  1069. can be inlined, introduced a new flag po_has_inlining_info
  1070. Revision 1.96 2004/11/09 17:26:47 peter
  1071. * fixed wrong typecasts
  1072. Revision 1.95 2004/11/08 22:09:59 peter
  1073. * tvarsym splitted
  1074. Revision 1.94 2004/11/01 23:30:11 peter
  1075. * support > 32bit accesses for x86_64
  1076. * rewrote array size checking to support 64bit
  1077. Revision 1.93 2004/11/01 15:32:12 peter
  1078. * support @labelsym
  1079. Revision 1.92 2004/10/15 09:14:17 mazen
  1080. - remove $IFDEF DELPHI and related code
  1081. - remove $IFDEF FPCPROCVAR and related code
  1082. Revision 1.91 2004/07/12 17:58:19 peter
  1083. * remove maxlen field from ansistring/widestrings
  1084. Revision 1.90 2004/07/03 14:06:35 daniel
  1085. * Compile fix
  1086. Revision 1.89 2004/06/20 20:41:47 florian
  1087. * fixed bootstrapping problems
  1088. Revision 1.88 2004/06/20 08:55:30 florian
  1089. * logs truncated
  1090. Revision 1.87 2004/06/18 15:16:46 peter
  1091. * remove obsolete cardinal() typecasts
  1092. Revision 1.86 2004/06/16 20:07:09 florian
  1093. * dwarf branch merged
  1094. Revision 1.85 2004/05/23 15:23:30 peter
  1095. * fixed qword(longint) that removed sign from the number
  1096. * removed code in the compiler that relied on wrong qword(longint)
  1097. code generation
  1098. Revision 1.84 2004/04/29 19:56:37 daniel
  1099. * Prepare compiler infrastructure for multiple ansistring types
  1100. Revision 1.83 2004/04/11 10:44:23 peter
  1101. * block_type is bt_const when parsing typed consts
  1102. }