ptconst.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. else
  425. Message(type_e_variable_id_expected);
  426. end;
  427. end
  428. else
  429. Message(parser_e_illegal_expression);
  430. end
  431. else
  432. { allow typeof(Object type)}
  433. if (p.nodetype=inlinen) and
  434. (tinlinenode(p).inlinenumber=in_typeof_x) then
  435. begin
  436. if (tinlinenode(p).left.nodetype=typen) then
  437. begin
  438. curconstSegment.concat(Tai_const.createname(
  439. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname,AT_DATA,0));
  440. end
  441. else
  442. Message(parser_e_illegal_expression);
  443. end
  444. else
  445. Message(parser_e_illegal_expression);
  446. p.free;
  447. end;
  448. setdef:
  449. begin
  450. p:=comp_expr(true);
  451. if p.nodetype=setconstn then
  452. begin
  453. { be sure to convert to the correct result, else
  454. it can generate smallset data instead of normalset (PFV) }
  455. inserttypeconv(p,t);
  456. { we only allow const sets }
  457. if assigned(tsetconstnode(p).left) then
  458. Message(parser_e_illegal_expression)
  459. else
  460. begin
  461. { this writing is endian independant }
  462. { untrue - because they are considered }
  463. { arrays of 32-bit values CEC }
  464. if source_info.endian = target_info.endian then
  465. begin
  466. for l:=0 to p.resulttype.def.size-1 do
  467. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[l]));
  468. end
  469. else
  470. begin
  471. { store as longint values in swaped format }
  472. j:=0;
  473. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  474. begin
  475. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  476. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  477. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  478. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  479. Inc(j,4);
  480. end;
  481. end;
  482. end;
  483. end
  484. else
  485. Message(parser_e_illegal_expression);
  486. p.free;
  487. end;
  488. enumdef:
  489. begin
  490. p:=comp_expr(true);
  491. if p.nodetype=ordconstn then
  492. begin
  493. if equal_defs(p.resulttype.def,t.def) or
  494. is_subequal(p.resulttype.def,t.def) then
  495. begin
  496. case longint(p.resulttype.def.size) of
  497. 1 : curconstSegment.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  498. 2 : curconstSegment.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  499. 4 : curconstSegment.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  500. end;
  501. end
  502. else
  503. IncompatibleTypes(p.resulttype.def,t.def);
  504. end
  505. else
  506. Message(parser_e_illegal_expression);
  507. p.free;
  508. end;
  509. stringdef:
  510. begin
  511. p:=comp_expr(true);
  512. { load strval and strlength of the constant tree }
  513. if (p.nodetype=stringconstn) or is_widestring(t.def) then
  514. begin
  515. { convert to the expected string type so that
  516. for widestrings strval is a pcompilerwidestring }
  517. inserttypeconv(p,t);
  518. strlength:=tstringconstnode(p).len;
  519. strval:=tstringconstnode(p).value_str;
  520. end
  521. else if is_constcharnode(p) then
  522. begin
  523. { strval:=pchar(@tordconstnode(p).value);
  524. THIS FAIL on BIG_ENDIAN MACHINES PM }
  525. c:=chr(tordconstnode(p).value and $ff);
  526. strval:=@c;
  527. strlength:=1
  528. end
  529. else if is_constresourcestringnode(p) then
  530. begin
  531. strval:=pchar(tconstsym(tloadnode(p).symtableentry).value.valueptr);
  532. strlength:=tconstsym(tloadnode(p).symtableentry).value.len;
  533. end
  534. else
  535. begin
  536. Message(parser_e_illegal_expression);
  537. strlength:=-1;
  538. end;
  539. if strlength>=0 then
  540. begin
  541. case tstringdef(t.def).string_typ of
  542. st_shortstring:
  543. begin
  544. if strlength>=t.def.size then
  545. begin
  546. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  547. strlength:=t.def.size-1;
  548. end;
  549. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  550. { this can also handle longer strings }
  551. getmem(ca,strlength+1);
  552. move(strval^,ca^,strlength);
  553. ca[strlength]:=#0;
  554. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  555. { fillup with spaces if size is shorter }
  556. if t.def.size>strlength then
  557. begin
  558. getmem(ca,t.def.size-strlength);
  559. { def.size contains also the leading length, so we }
  560. { we have to subtract one }
  561. fillchar(ca[0],t.def.size-strlength-1,' ');
  562. ca[t.def.size-strlength-1]:=#0;
  563. { this can also handle longer strings }
  564. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  565. end;
  566. end;
  567. {$ifdef ansistrings_bits}
  568. st_ansistring16:
  569. begin
  570. { an empty ansi string is nil! }
  571. if (strlength=0) then
  572. curconstSegment.concat(Tai_const.Create_ptr(0))
  573. else
  574. begin
  575. objectlibrary.getdatalabel(ll);
  576. curconstSegment.concat(Tai_const_symbol.Create(ll));
  577. { the actual structure starts at -12 from start label - CEC }
  578. Consts.concat(tai_align.create(const_align(pointer_size)));
  579. { first write the maximum size }
  580. Consts.concat(Tai_const.Create_16bit(strlength));
  581. { second write the real length }
  582. Consts.concat(Tai_const.Create_16bit(strlength));
  583. { redondent with maxlength but who knows ... (PM) }
  584. { third write use count (set to -1 for safety ) }
  585. Consts.concat(Tai_const.Create_16bit(-1));
  586. Consts.concat(Tai_label.Create(ll));
  587. getmem(ca,strlength+2);
  588. move(strval^,ca^,strlength);
  589. { The terminating #0 to be stored in the .data section (JM) }
  590. ca[strlength]:=#0;
  591. { End of the PChar. The memory has to be allocated because in }
  592. { tai_string.done, there is a freemem(len+1) (JM) }
  593. ca[strlength+1]:=#0;
  594. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  595. end;
  596. end;
  597. {$endif}
  598. {$ifdef ansistring_bits}st_ansistring32{$else}st_ansistring{$endif}:
  599. begin
  600. { an empty ansi string is nil! }
  601. if (strlength=0) then
  602. curconstSegment.concat(Tai_const.Create_sym(nil))
  603. else
  604. begin
  605. objectlibrary.getdatalabel(ll);
  606. curconstSegment.concat(Tai_const.Create_sym(ll));
  607. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  608. Consts.concat(Tai_const.Create_aint(-1));
  609. Consts.concat(Tai_const.Create_aint(strlength));
  610. Consts.concat(Tai_label.Create(ll));
  611. getmem(ca,strlength+2);
  612. move(strval^,ca^,strlength);
  613. { The terminating #0 to be stored in the .data section (JM) }
  614. ca[strlength]:=#0;
  615. { End of the PChar. The memory has to be allocated because in }
  616. { tai_string.done, there is a freemem(len+1) (JM) }
  617. ca[strlength+1]:=#0;
  618. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  619. end;
  620. end;
  621. {$ifdef ansistring_bits}
  622. st_ansistring64:
  623. begin
  624. { an empty ansi string is nil! }
  625. if (strlength=0) then
  626. curconstSegment.concat(Tai_const.Create_ptr(0))
  627. else
  628. begin
  629. objectlibrary.getdatalabel(ll);
  630. curconstSegment.concat(Tai_const_symbol.Create(ll));
  631. { the actual structure starts at -12 from start label - CEC }
  632. Consts.concat(tai_align.create(const_align(pointer_size)));
  633. { first write the maximum size }
  634. Consts.concat(Tai_const.Create_64bit(strlength));
  635. { second write the real length }
  636. Consts.concat(Tai_const.Create_64bit(strlength));
  637. { redondent with maxlength but who knows ... (PM) }
  638. { third write use count (set to -1 for safety ) }
  639. Consts.concat(Tai_const.Create_64bit(-1));
  640. Consts.concat(Tai_label.Create(ll));
  641. getmem(ca,strlength+2);
  642. move(strval^,ca^,strlength);
  643. { The terminating #0 to be stored in the .data section (JM) }
  644. ca[strlength]:=#0;
  645. { End of the PChar. The memory has to be allocated because in }
  646. { tai_string.done, there is a freemem(len+1) (JM) }
  647. ca[strlength+1]:=#0;
  648. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  649. end;
  650. end;
  651. {$endif}
  652. st_widestring:
  653. begin
  654. { an empty ansi string is nil! }
  655. if (strlength=0) then
  656. curconstSegment.concat(Tai_const.Create_sym(nil))
  657. else
  658. begin
  659. objectlibrary.getdatalabel(ll);
  660. curconstSegment.concat(Tai_const.Create_sym(ll));
  661. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  662. Consts.concat(Tai_const.Create_aint(-1));
  663. Consts.concat(Tai_const.Create_aint(strlength));
  664. Consts.concat(Tai_label.Create(ll));
  665. for i:=0 to strlength-1 do
  666. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  667. { ending #0 }
  668. Consts.concat(Tai_const.Create_16bit(0))
  669. end;
  670. end;
  671. st_longstring:
  672. begin
  673. internalerror(200107081);
  674. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  675. curconstSegment.concat(Tai_const.Create_8bit(0));
  676. getmem(ca,strlength+1);
  677. move(strval^,ca^,strlength);
  678. ca[strlength]:=#0;
  679. generate_pascii(consts,ca,strlength);
  680. curconstSegment.concat(Tai_const.Create_8bit(0));}
  681. end;
  682. end;
  683. end;
  684. p.free;
  685. end;
  686. arraydef:
  687. begin
  688. if try_to_consume(_LKLAMMER) then
  689. begin
  690. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  691. begin
  692. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  693. consume(_COMMA);
  694. end;
  695. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  696. consume(_RKLAMMER);
  697. end
  698. else
  699. { if array of char then we allow also a string }
  700. if is_char(tarraydef(t.def).elementtype.def) then
  701. begin
  702. p:=comp_expr(true);
  703. if p.nodetype=stringconstn then
  704. begin
  705. len:=tstringconstnode(p).len;
  706. { For tp7 the maximum lentgh can be 255 }
  707. if (m_tp7 in aktmodeswitches) and
  708. (len>255) then
  709. len:=255;
  710. ca:=tstringconstnode(p).value_str;
  711. end
  712. else
  713. if is_constcharnode(p) then
  714. begin
  715. c:=chr(tordconstnode(p).value and $ff);
  716. ca:=@c;
  717. len:=1;
  718. end
  719. else
  720. begin
  721. Message(parser_e_illegal_expression);
  722. len:=0;
  723. end;
  724. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  725. Message(parser_e_string_larger_array);
  726. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  727. begin
  728. if i+1-tarraydef(t.def).lowrange<=len then
  729. begin
  730. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  731. inc(ca);
  732. end
  733. else
  734. {Fill the remaining positions with #0.}
  735. curconstSegment.concat(Tai_const.Create_8bit(0));
  736. end;
  737. p.free;
  738. end
  739. else
  740. { dynamic array nil }
  741. if is_dynamic_array(t.def) then
  742. begin
  743. { Only allow nil initialization }
  744. consume(_NIL);
  745. curconstSegment.concat(Tai_const.Create_sym(nil));
  746. end
  747. else
  748. begin
  749. { we want the ( }
  750. consume(_LKLAMMER);
  751. end;
  752. end;
  753. procvardef:
  754. begin
  755. { Procvars and pointers are no longer compatible. }
  756. { under tp: =nil or =var under fpc: =nil or =@var }
  757. if token=_NIL then
  758. begin
  759. curconstSegment.concat(Tai_const.Create_sym(nil));
  760. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  761. curconstSegment.concat(Tai_const.Create_sym(nil));
  762. consume(_NIL);
  763. exit;
  764. end;
  765. { you can't assign a value other than NIL to a typed constant }
  766. { which is a "procedure of object", because this also requires }
  767. { address of an object/class instance, which is not known at }
  768. { compile time (JM) }
  769. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  770. Message(parser_e_no_procvarobj_const);
  771. { parse the rest too, so we can continue with error checking }
  772. getprocvardef:=tprocvardef(t.def);
  773. p:=comp_expr(true);
  774. getprocvardef:=nil;
  775. if codegenerror then
  776. begin
  777. p.free;
  778. exit;
  779. end;
  780. { let type conversion check everything needed }
  781. inserttypeconv(p,t);
  782. if codegenerror then
  783. begin
  784. p.free;
  785. exit;
  786. end;
  787. { remove typeconvn, that will normally insert a lea
  788. instruction which is not necessary for us }
  789. if p.nodetype=typeconvn then
  790. begin
  791. hp:=ttypeconvnode(p).left;
  792. ttypeconvnode(p).left:=nil;
  793. p.free;
  794. p:=hp;
  795. end;
  796. { remove addrn which we also don't need here }
  797. if p.nodetype=addrn then
  798. begin
  799. hp:=taddrnode(p).left;
  800. taddrnode(p).left:=nil;
  801. p.free;
  802. p:=hp;
  803. end;
  804. { we now need to have a loadn with a procsym }
  805. if (p.nodetype=loadn) and
  806. (tloadnode(p).symtableentry.typ=procsym) then
  807. begin
  808. curconstSegment.concat(Tai_const.createname(
  809. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,AT_FUNCTION,0));
  810. end
  811. else
  812. Message(parser_e_illegal_expression);
  813. p.free;
  814. end;
  815. { reads a typed constant record }
  816. recorddef:
  817. begin
  818. { KAZ }
  819. if (trecorddef(t.def)=rec_tguid) and
  820. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  821. begin
  822. p:=comp_expr(true);
  823. inserttypeconv(p,cshortstringtype);
  824. if p.nodetype=stringconstn then
  825. begin
  826. s:=strpas(tstringconstnode(p).value_str);
  827. p.free;
  828. if string2guid(s,tmpguid) then
  829. begin
  830. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  831. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  832. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  833. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  834. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  835. end
  836. else
  837. Message(parser_e_improper_guid_syntax);
  838. end
  839. else
  840. begin
  841. p.free;
  842. Message(parser_e_illegal_expression);
  843. exit;
  844. end;
  845. end
  846. else
  847. begin
  848. consume(_LKLAMMER);
  849. sorg:='';
  850. aktpos:=0;
  851. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  852. recsym := nil;
  853. while token<>_RKLAMMER do
  854. begin
  855. s:=pattern;
  856. sorg:=orgpattern;
  857. consume(_ID);
  858. consume(_COLON);
  859. error := false;
  860. recsym := tsym(trecorddef(t.def).symtable.search(s));
  861. if not assigned(recsym) then
  862. begin
  863. Message1(sym_e_illegal_field,sorg);
  864. error := true;
  865. end;
  866. if (not error) and
  867. (not assigned(srsym) or
  868. (s <> srsym.name)) then
  869. { possible variant record (JM) }
  870. begin
  871. { All parts of a variant start at the same offset }
  872. { Also allow jumping from one variant part to another, }
  873. { as long as the offsets match }
  874. if (assigned(srsym) and
  875. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  876. { srsym is not assigned after parsing w2 in the }
  877. { typed const in the next example: }
  878. { type tr = record case byte of }
  879. { 1: (l1,l2: dword); }
  880. { 2: (w1,w2: word); }
  881. { end; }
  882. { const r: tr = (w1:1;w2:1;l2:5); }
  883. (tfieldvarsym(recsym).fieldoffset = aktpos) then
  884. srsym := recsym
  885. { going backwards isn't allowed in any mode }
  886. else if (tfieldvarsym(recsym).fieldoffset<aktpos) then
  887. begin
  888. Message(parser_e_invalid_record_const);
  889. error := true;
  890. end
  891. { Delphi allows you to skip fields }
  892. else if (m_delphi in aktmodeswitches) then
  893. begin
  894. Message1(parser_w_skipped_fields_before,sorg);
  895. srsym := recsym;
  896. end
  897. { FPC and TP don't }
  898. else
  899. begin
  900. Message1(parser_e_skipped_fields_before,sorg);
  901. error := true;
  902. end;
  903. end;
  904. if error then
  905. consume_all_until(_SEMICOLON)
  906. else
  907. begin
  908. { if needed fill (alignment) }
  909. if tfieldvarsym(srsym).fieldoffset>aktpos then
  910. for i:=1 to tfieldvarsym(srsym).fieldoffset-aktpos do
  911. curconstSegment.concat(Tai_const.Create_8bit(0));
  912. { new position }
  913. aktpos:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vartype.def.size;
  914. { read the data }
  915. readtypedconst(tfieldvarsym(srsym).vartype,nil,writable);
  916. { keep previous field for checking whether whole }
  917. { record was initialized (JM) }
  918. recsym := srsym;
  919. { goto next field }
  920. srsym := tsym(srsym.indexnext);
  921. if token=_SEMICOLON then
  922. consume(_SEMICOLON)
  923. else break;
  924. end;
  925. end;
  926. { are there any fields left? }
  927. if assigned(srsym) and
  928. { don't complain if there only come other variant parts }
  929. { after the last initialized field }
  930. ((recsym=nil) or
  931. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)) then
  932. Message1(parser_w_skipped_fields_after,sorg);
  933. for i:=1 to t.def.size-aktpos do
  934. curconstSegment.concat(Tai_const.Create_8bit(0));
  935. consume(_RKLAMMER);
  936. end;
  937. end;
  938. { reads a typed object }
  939. objectdef:
  940. begin
  941. if is_class_or_interface(t.def) then
  942. begin
  943. p:=comp_expr(true);
  944. if p.nodetype<>niln then
  945. begin
  946. Message(parser_e_type_const_not_possible);
  947. consume_all_until(_RKLAMMER);
  948. end
  949. else
  950. begin
  951. curconstSegment.concat(Tai_const.Create_sym(nil));
  952. end;
  953. p.free;
  954. end
  955. { for objects we allow it only if it doesn't contain a vmt }
  956. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  957. (m_fpc in aktmodeswitches) then
  958. Message(parser_e_type_const_not_possible)
  959. else
  960. begin
  961. consume(_LKLAMMER);
  962. aktpos:=0;
  963. while token<>_RKLAMMER do
  964. begin
  965. s:=pattern;
  966. sorg:=orgpattern;
  967. consume(_ID);
  968. consume(_COLON);
  969. srsym:=nil;
  970. obj:=tobjectdef(t.def);
  971. symt:=obj.symtable;
  972. while (srsym=nil) and assigned(symt) do
  973. begin
  974. srsym:=tsym(symt.search(s));
  975. if assigned(obj) then
  976. obj:=obj.childof;
  977. if assigned(obj) then
  978. symt:=obj.symtable
  979. else
  980. symt:=nil;
  981. end;
  982. if srsym=nil then
  983. begin
  984. Message1(sym_e_id_not_found,sorg);
  985. consume_all_until(_SEMICOLON);
  986. end
  987. else
  988. with tfieldvarsym(srsym) do
  989. begin
  990. { check position }
  991. if fieldoffset<aktpos then
  992. message(parser_e_invalid_record_const);
  993. { check in VMT needs to be added for TP mode }
  994. with Tobjectdef(t.def) do
  995. if not(m_fpc in aktmodeswitches) and
  996. (oo_has_vmt in objectoptions) and
  997. (vmt_offset<fieldoffset) then
  998. begin
  999. for i:=1 to vmt_offset-aktpos do
  1000. curconstsegment.concat(tai_const.create_8bit(0));
  1001. curconstsegment.concat(tai_const.createname(vmt_mangledname,AT_DATA,0));
  1002. { this is more general }
  1003. aktpos:=vmt_offset + sizeof(aint);
  1004. end;
  1005. { if needed fill }
  1006. if fieldoffset>aktpos then
  1007. for i:=1 to fieldoffset-aktpos do
  1008. curconstSegment.concat(Tai_const.Create_8bit(0));
  1009. { new position }
  1010. aktpos:=fieldoffset+vartype.def.size;
  1011. { read the data }
  1012. readtypedconst(vartype,nil,writable);
  1013. if token=_SEMICOLON then
  1014. consume(_SEMICOLON)
  1015. else break;
  1016. end;
  1017. end;
  1018. if not(m_fpc in aktmodeswitches) and
  1019. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  1020. (tobjectdef(t.def).vmt_offset>=aktpos) then
  1021. begin
  1022. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  1023. curconstsegment.concat(tai_const.create_8bit(0));
  1024. curconstsegment.concat(tai_const.createname(tobjectdef(t.def).vmt_mangledname,AT_DATA,0));
  1025. { this is more general }
  1026. aktpos:=tobjectdef(t.def).vmt_offset + sizeof(aint);
  1027. end;
  1028. for i:=1 to t.def.size-aktpos do
  1029. curconstSegment.concat(Tai_const.Create_8bit(0));
  1030. consume(_RKLAMMER);
  1031. end;
  1032. end;
  1033. errordef:
  1034. begin
  1035. { try to consume something useful }
  1036. if token=_LKLAMMER then
  1037. consume_all_until(_RKLAMMER)
  1038. else
  1039. consume_all_until(_SEMICOLON);
  1040. end;
  1041. else Message(parser_e_type_const_not_possible);
  1042. end;
  1043. block_type:=old_block_type;
  1044. end;
  1045. {$ifdef fpc}
  1046. {$maxfpuregisters default}
  1047. {$endif fpc}
  1048. end.
  1049. {
  1050. $Log$
  1051. Revision 1.97 2004-12-05 12:28:11 peter
  1052. * procvar handling for tp procvar mode fixed
  1053. * proc to procvar moved from addrnode to typeconvnode
  1054. * inlininginfo is now allocated only for inline routines that
  1055. can be inlined, introduced a new flag po_has_inlining_info
  1056. Revision 1.96 2004/11/09 17:26:47 peter
  1057. * fixed wrong typecasts
  1058. Revision 1.95 2004/11/08 22:09:59 peter
  1059. * tvarsym splitted
  1060. Revision 1.94 2004/11/01 23:30:11 peter
  1061. * support > 32bit accesses for x86_64
  1062. * rewrote array size checking to support 64bit
  1063. Revision 1.93 2004/11/01 15:32:12 peter
  1064. * support @labelsym
  1065. Revision 1.92 2004/10/15 09:14:17 mazen
  1066. - remove $IFDEF DELPHI and related code
  1067. - remove $IFDEF FPCPROCVAR and related code
  1068. Revision 1.91 2004/07/12 17:58:19 peter
  1069. * remove maxlen field from ansistring/widestrings
  1070. Revision 1.90 2004/07/03 14:06:35 daniel
  1071. * Compile fix
  1072. Revision 1.89 2004/06/20 20:41:47 florian
  1073. * fixed bootstrapping problems
  1074. Revision 1.88 2004/06/20 08:55:30 florian
  1075. * logs truncated
  1076. Revision 1.87 2004/06/18 15:16:46 peter
  1077. * remove obsolete cardinal() typecasts
  1078. Revision 1.86 2004/06/16 20:07:09 florian
  1079. * dwarf branch merged
  1080. Revision 1.85 2004/05/23 15:23:30 peter
  1081. * fixed qword(longint) that removed sign from the number
  1082. * removed code in the compiler that relied on wrong qword(longint)
  1083. code generation
  1084. Revision 1.84 2004/04/29 19:56:37 daniel
  1085. * Prepare compiler infrastructure for multiple ansistring types
  1086. Revision 1.83 2004/04/11 10:44:23 peter
  1087. * block_type is bt_const when parsing typed consts
  1088. }