ptconst.pas 49 KB

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