ptconst.pas 48 KB

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