ptconst.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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,aasmdata;
  21. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,systems,tokens,verbose,
  26. cutils,globals,widestr,scanner,
  27. symconst,symbase,symdef,symtable,
  28. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  29. { pass 1 }
  30. node,htypechk,procinfo,
  31. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  32. { parser specific stuff }
  33. pbase,pexpr,
  34. { codegen }
  35. cpuinfo,cgbase,dbgbase
  36. ;
  37. {$maxfpuregisters 0}
  38. {*****************************************************************************
  39. Bitpacked value helpers
  40. *****************************************************************************}
  41. type
  42. tbitpackedval = record
  43. curval, nextval: aword;
  44. curbitoffset: smallint;
  45. loadbitsize,packedbitsize: byte;
  46. end;
  47. procedure initbitpackval(out bp: tbitpackedval; packedbitsize: byte);
  48. begin
  49. bp.curval:=0;
  50. bp.nextval:=0;
  51. bp.curbitoffset:=0;
  52. bp.packedbitsize:=packedbitsize;
  53. bp.loadbitsize:=packedbitsloadsize(bp.packedbitsize)*8;
  54. end;
  55. {$ifopt r+}
  56. {$define rangeon}
  57. {$r-}
  58. {$endif}
  59. {$ifopt q+}
  60. {$define overflowon}
  61. {$q-}
  62. {$endif}
  63. { (values between quotes below refer to fields of bp; fields not }
  64. { mentioned are unused by this routine) }
  65. { bitpacks "value" as bitpacked value of bitsize "packedbitsize" into }
  66. { "curval", which has already been filled up to "curbitoffset", and }
  67. { stores the spillover if any into "nextval". It also updates }
  68. { curbitoffset to reflect how many bits of currval are now used (can be }
  69. { > AIntBits in case of spillover) }
  70. procedure bitpackval(value: aword; var bp: tbitpackedval);
  71. var
  72. shiftcount: longint;
  73. begin
  74. if (target_info.endian=endian_big) then
  75. begin
  76. { bitpacked format: left-aligned (i.e., "big endian bitness") }
  77. bp.curval:=bp.curval or ((value shl (AIntBits-bp.packedbitsize)) shr bp.curbitoffset);
  78. shiftcount:=((AIntBits-bp.packedbitsize)-bp.curbitoffset);
  79. { carry-over to the next element? }
  80. if (shiftcount<0) then
  81. bp.nextval:=(value and ((aword(1) shl (-shiftcount))-1)) shl
  82. (AIntBits+shiftcount)
  83. end
  84. else
  85. begin
  86. { bitpacked format: right aligned (i.e., "little endian bitness") }
  87. bp.curval:=bp.curval or (value shl bp.curbitoffset);
  88. { carry-over to the next element? }
  89. if (bp.curbitoffset+bp.packedbitsize>AIntBits) then
  90. bp.nextval:=value shr (AIntBits-bp.curbitoffset)
  91. end;
  92. inc(bp.curbitoffset,bp.packedbitsize);
  93. end;
  94. {$ifdef rangeon}
  95. {$r+}
  96. {$undef rangeon}
  97. {$endif}
  98. {$ifdef overflowon}
  99. {$q+}
  100. {$undef overflowon}
  101. {$endif}
  102. procedure flush_packed_value(list: tasmlist; var bp: tbitpackedval);
  103. var
  104. bitstowrite: longint;
  105. writeval : byte;
  106. begin
  107. if (bp.curbitoffset < AIntBits) then
  108. begin
  109. { forced flush -> write multiple of loadsize }
  110. bitstowrite:=align(bp.curbitoffset,bp.loadbitsize);
  111. bp.curbitoffset:=0;
  112. end
  113. else
  114. begin
  115. bitstowrite:=AIntBits;
  116. dec(bp.curbitoffset,AIntBits);
  117. end;
  118. while (bitstowrite>=8) do
  119. begin
  120. if (target_info.endian=endian_little) then
  121. begin
  122. { write lowest byte }
  123. writeval:=byte(bp.curval);
  124. bp.curval:=bp.curval shr 8;
  125. end
  126. else
  127. begin
  128. { write highest byte }
  129. writeval:=bp.curval shr (AIntBits-8);
  130. bp.curval:=(bp.curval and (not($ff shl (AIntBits-8)))) shl 8;
  131. end;
  132. list.concat(tai_const.create_8bit(writeval));
  133. dec(bitstowrite,8);
  134. end;
  135. bp.curval:=bp.nextval;
  136. bp.nextval:=0;
  137. end;
  138. {*****************************************************************************
  139. read typed const
  140. *****************************************************************************}
  141. { this procedure reads typed constants }
  142. procedure read_typed_const_data(list:tasmlist;def:tdef);
  143. procedure parse_orddef(list:tasmlist;def:torddef);
  144. var
  145. n : tnode;
  146. intvalue : tconstexprint;
  147. begin
  148. n:=comp_expr(true);
  149. case def.ordtype of
  150. bool8bit :
  151. begin
  152. if is_constboolnode(n) then
  153. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value)))
  154. else
  155. IncompatibleTypes(n.resultdef, def);
  156. end;
  157. bool16bit :
  158. begin
  159. if is_constboolnode(n) then
  160. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value)))
  161. else
  162. IncompatibleTypes(n.resultdef, def);
  163. end;
  164. bool32bit :
  165. begin
  166. if is_constboolnode(n) then
  167. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value)))
  168. else
  169. IncompatibleTypes(n.resultdef, def);
  170. end;
  171. bool64bit :
  172. begin
  173. if is_constboolnode(n) then
  174. list.concat(Tai_const.Create_64bit(int64(tordconstnode(n).value)))
  175. else
  176. IncompatibleTypes(n.resultdef, def);
  177. end;
  178. uchar :
  179. begin
  180. if is_constcharnode(n) or
  181. ((m_delphi in current_settings.modeswitches) and
  182. is_constwidecharnode(n) and
  183. (tordconstnode(n).value <= 255)) then
  184. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value)))
  185. else
  186. IncompatibleTypes(n.resultdef, def);
  187. end;
  188. uwidechar :
  189. begin
  190. if is_constcharnode(n) then
  191. inserttypeconv(n,cwidechartype);
  192. if is_constwidecharnode(n) then
  193. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value)))
  194. else
  195. IncompatibleTypes(n.resultdef, def);
  196. end;
  197. s8bit,u8bit,
  198. u16bit,s16bit,
  199. s32bit,u32bit,
  200. s64bit,u64bit :
  201. begin
  202. if is_constintnode(n) then
  203. begin
  204. testrange(n.resultdef,def,tordconstnode(n).value,false);
  205. case def.size of
  206. 1 :
  207. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value)));
  208. 2 :
  209. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value)));
  210. 4 :
  211. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value)));
  212. 8 :
  213. list.concat(Tai_const.Create_64bit(tordconstnode(n).value));
  214. end;
  215. end
  216. else
  217. IncompatibleTypes(n.resultdef, def);
  218. end;
  219. scurrency:
  220. begin
  221. if is_constintnode(n) then
  222. intvalue := tordconstnode(n).value
  223. { allow bootstrapping }
  224. else if is_constrealnode(n) then
  225. intvalue:=PInt64(@trealconstnode(n).value_currency)^
  226. else
  227. begin
  228. intvalue:=0;
  229. IncompatibleTypes(n.resultdef, def);
  230. end;
  231. list.concat(Tai_const.Create_64bit(intvalue));
  232. end;
  233. else
  234. internalerror(200611052);
  235. end;
  236. n.free;
  237. end;
  238. procedure parse_floatdef(list:tasmlist;def:tfloatdef);
  239. var
  240. n : tnode;
  241. value : bestreal;
  242. begin
  243. n:=comp_expr(true);
  244. if is_constrealnode(n) then
  245. value:=trealconstnode(n).value_real
  246. else if is_constintnode(n) then
  247. value:=tordconstnode(n).value
  248. else
  249. IncompatibleTypes(n.resultdef, def);
  250. case def.floattype of
  251. s32real :
  252. list.concat(Tai_real_32bit.Create(ts32real(value)));
  253. s64real :
  254. {$ifdef ARM}
  255. if (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  256. not(cs_fp_emulation in current_settings.moduleswitches) then
  257. list.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  258. else
  259. {$endif ARM}
  260. list.concat(Tai_real_64bit.Create(ts64real(value)));
  261. s80real :
  262. list.concat(Tai_real_80bit.Create(value));
  263. s64comp :
  264. { the round is necessary for native compilers where comp isn't a float }
  265. list.concat(Tai_comp_64bit.Create(round(value)));
  266. s64currency:
  267. list.concat(Tai_comp_64bit.Create(round(value*10000)));
  268. s128real:
  269. list.concat(Tai_real_128bit.Create(value));
  270. else
  271. internalerror(200611053);
  272. end;
  273. n.free;
  274. end;
  275. procedure parse_classrefdef(list:tasmlist;def:tclassrefdef);
  276. var
  277. n : tnode;
  278. begin
  279. n:=comp_expr(true);
  280. case n.nodetype of
  281. loadvmtaddrn:
  282. begin
  283. if not Tobjectdef(tclassrefdef(n.resultdef).pointeddef).is_related(tobjectdef(def.pointeddef)) then
  284. IncompatibleTypes(n.resultdef, def);
  285. list.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(n.resultdef).pointeddef).vmt_mangledname)));
  286. end;
  287. niln:
  288. list.concat(Tai_const.Create_sym(nil));
  289. else
  290. IncompatibleTypes(n.resultdef, def);
  291. end;
  292. n.free;
  293. end;
  294. procedure parse_pointerdef(list:tasmlist;def:tpointerdef);
  295. var
  296. hp,p : tnode;
  297. srsym : tsym;
  298. pd : tprocdef;
  299. ca : pchar;
  300. pw : pcompilerwidestring;
  301. i,len : longint;
  302. base,
  303. offset : aint;
  304. ll : tasmlabel;
  305. varalign : shortint;
  306. begin
  307. p:=comp_expr(true);
  308. { remove equal typecasts for pointer/nil addresses }
  309. if (p.nodetype=typeconvn) then
  310. with Ttypeconvnode(p) do
  311. if (left.nodetype in [addrn,niln]) and equal_defs(def,p.resultdef) then
  312. begin
  313. hp:=left;
  314. left:=nil;
  315. p.free;
  316. p:=hp;
  317. end;
  318. { allows horrible ofs(typeof(TButton)^) code !! }
  319. if (p.nodetype=addrn) then
  320. with Taddrnode(p) do
  321. if left.nodetype=derefn then
  322. begin
  323. hp:=tderefnode(left).left;
  324. tderefnode(left).left:=nil;
  325. p.free;
  326. p:=hp;
  327. end;
  328. { const pointer ? }
  329. if (p.nodetype = pointerconstn) then
  330. begin
  331. if sizeof(TConstPtrUInt)=8 then
  332. list.concat(Tai_const.Create_64bit(int64(tpointerconstnode(p).value)))
  333. else
  334. if sizeof(TConstPtrUInt)=4 then
  335. list.concat(Tai_const.Create_32bit(longint(tpointerconstnode(p).value)))
  336. else
  337. internalerror(200404122);
  338. end
  339. { nil pointer ? }
  340. else if p.nodetype=niln then
  341. list.concat(Tai_const.Create_sym(nil))
  342. { maybe pchar ? }
  343. else
  344. if is_char(def.pointeddef) and
  345. (p.nodetype<>addrn) then
  346. begin
  347. current_asmdata.getdatalabel(ll);
  348. list.concat(Tai_const.Create_sym(ll));
  349. if p.nodetype=stringconstn then
  350. varalign:=size_2_align(tstringconstnode(p).len)
  351. else
  352. varalign:=0;
  353. varalign:=const_align(varalign);
  354. current_asmdata.asmlists[al_const].concat(Tai_align.Create(varalign));
  355. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  356. if p.nodetype=stringconstn then
  357. begin
  358. len:=tstringconstnode(p).len;
  359. { For tp7 the maximum lentgh can be 255 }
  360. if (m_tp7 in current_settings.modeswitches) and
  361. (len>255) then
  362. len:=255;
  363. getmem(ca,len+2);
  364. move(tstringconstnode(p).value_str^,ca^,len+1);
  365. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  366. end
  367. else
  368. if is_constcharnode(p) then
  369. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  370. else
  371. IncompatibleTypes(p.resultdef, def);
  372. end
  373. { maybe pwidechar ? }
  374. else
  375. if is_widechar(def.pointeddef) and
  376. (p.nodetype<>addrn) then
  377. begin
  378. current_asmdata.getdatalabel(ll);
  379. list.concat(Tai_const.Create_sym(ll));
  380. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(aint))));
  381. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  382. if (p.nodetype in [stringconstn,ordconstn]) then
  383. begin
  384. { convert to widestring stringconstn }
  385. inserttypeconv(p,cwidestringtype);
  386. if (p.nodetype=stringconstn) and
  387. (tstringconstnode(p).cst_type=cst_widestring) then
  388. begin
  389. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  390. for i:=0 to tstringconstnode(p).len-1 do
  391. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  392. { ending #0 }
  393. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  394. end;
  395. end
  396. else
  397. IncompatibleTypes(p.resultdef, def);
  398. end
  399. else
  400. if (p.nodetype=addrn) or
  401. is_procvar_load(p) then
  402. begin
  403. { insert typeconv }
  404. inserttypeconv(p,def);
  405. hp:=p;
  406. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  407. hp:=tunarynode(hp).left;
  408. if (hp.nodetype=loadn) then
  409. begin
  410. hp:=p;
  411. offset:=0;
  412. while assigned(hp) and (hp.nodetype<>loadn) do
  413. begin
  414. case hp.nodetype of
  415. vecn :
  416. begin
  417. case tvecnode(hp).left.resultdef.typ of
  418. stringdef :
  419. begin
  420. { this seems OK for shortstring and ansistrings PM }
  421. { it is wrong for widestrings !! }
  422. len:=1;
  423. base:=0;
  424. end;
  425. arraydef :
  426. begin
  427. if not is_packed_array(tvecnode(hp).left.resultdef) then
  428. begin
  429. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  430. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  431. end
  432. else
  433. begin
  434. Message(parser_e_packed_dynamic_open_array);
  435. len:=1;
  436. base:=0;
  437. end;
  438. end
  439. else
  440. Message(parser_e_illegal_expression);
  441. end;
  442. if is_constintnode(tvecnode(hp).right) then
  443. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  444. else
  445. Message(parser_e_illegal_expression);
  446. end;
  447. subscriptn :
  448. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  449. typeconvn :
  450. begin
  451. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  452. Message(parser_e_illegal_expression);
  453. end;
  454. addrn :
  455. ;
  456. else
  457. Message(parser_e_illegal_expression);
  458. end;
  459. hp:=tunarynode(hp).left;
  460. end;
  461. srsym:=tloadnode(hp).symtableentry;
  462. case srsym.typ of
  463. procsym :
  464. begin
  465. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  466. if Tprocsym(srsym).ProcdefList.Count>1 then
  467. Message(parser_e_no_overloaded_procvars);
  468. if po_abstractmethod in pd.procoptions then
  469. Message(type_e_cant_take_address_of_abstract_method)
  470. else
  471. list.concat(Tai_const.Createname(pd.mangledname,offset));
  472. end;
  473. staticvarsym :
  474. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  475. labelsym :
  476. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  477. constsym :
  478. if tconstsym(srsym).consttyp=constresourcestring then
  479. list.concat(Tai_const.Createname(make_mangledname('RESSTR',tconstsym(srsym).owner,tconstsym(srsym).name),sizeof(aint)))
  480. else
  481. Message(type_e_variable_id_expected);
  482. else
  483. Message(type_e_variable_id_expected);
  484. end;
  485. end
  486. else
  487. Message(parser_e_illegal_expression);
  488. end
  489. else
  490. { allow typeof(Object type)}
  491. if (p.nodetype=inlinen) and
  492. (tinlinenode(p).inlinenumber=in_typeof_x) then
  493. begin
  494. if (tinlinenode(p).left.nodetype=typen) then
  495. begin
  496. list.concat(Tai_const.createname(
  497. tobjectdef(tinlinenode(p).left.resultdef).vmt_mangledname,0));
  498. end
  499. else
  500. Message(parser_e_illegal_expression);
  501. end
  502. else
  503. Message(parser_e_illegal_expression);
  504. p.free;
  505. end;
  506. procedure parse_setdef(list:tasmlist;def:tsetdef);
  507. type
  508. setbytes = array[0..31] of byte;
  509. Psetbytes = ^setbytes;
  510. var
  511. p : tnode;
  512. i : longint;
  513. begin
  514. p:=comp_expr(true);
  515. if p.nodetype=setconstn then
  516. begin
  517. { be sure to convert to the correct result, else
  518. it can generate smallset data instead of normalset (PFV) }
  519. inserttypeconv(p,def);
  520. { we only allow const sets }
  521. if assigned(tsetconstnode(p).left) then
  522. Message(parser_e_illegal_expression)
  523. else
  524. begin
  525. { this writing is endian independant }
  526. { untrue - because they are considered }
  527. { arrays of 32-bit values CEC }
  528. if source_info.endian = target_info.endian then
  529. begin
  530. {$if defined(FPC_NEW_BIGENDIAN_SETS) or defined(FPC_LITTLE_ENDIAN)}
  531. for i:=0 to p.resultdef.size-1 do
  532. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[i]));
  533. {$else}
  534. for i:=0 to p.resultdef.size-1 do
  535. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(p).value_set)^[i xor 3])));
  536. {$endif}
  537. end
  538. else
  539. begin
  540. for i:=0 to p.resultdef.size-1 do
  541. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(p).value_set)^[i])));
  542. end;
  543. end;
  544. end
  545. else
  546. Message(parser_e_illegal_expression);
  547. p.free;
  548. end;
  549. procedure parse_enumdef(list:tasmlist;def:tenumdef);
  550. var
  551. p : tnode;
  552. begin
  553. p:=comp_expr(true);
  554. if p.nodetype=ordconstn then
  555. begin
  556. if equal_defs(p.resultdef,def) or
  557. is_subequal(p.resultdef,def) then
  558. begin
  559. case longint(p.resultdef.size) of
  560. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  561. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  562. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  563. end;
  564. end
  565. else
  566. IncompatibleTypes(p.resultdef,def);
  567. end
  568. else
  569. Message(parser_e_illegal_expression);
  570. p.free;
  571. end;
  572. procedure parse_stringdef(list:tasmlist;def:tstringdef);
  573. var
  574. n : tnode;
  575. i : longint;
  576. strlength : aint;
  577. strval : pchar;
  578. strch : char;
  579. ll : tasmlabel;
  580. ca : pchar;
  581. begin
  582. n:=comp_expr(true);
  583. { load strval and strlength of the constant tree }
  584. if (n.nodetype=stringconstn) or is_widestring(def) then
  585. begin
  586. { convert to the expected string type so that
  587. for widestrings strval is a pcompilerwidestring }
  588. inserttypeconv(n,def);
  589. strlength:=tstringconstnode(n).len;
  590. strval:=tstringconstnode(n).value_str;
  591. end
  592. else if is_constcharnode(n) then
  593. begin
  594. { strval:=pchar(@tordconstnode(n).value);
  595. THIS FAIL on BIG_ENDIAN MACHINES PM }
  596. strch:=chr(tordconstnode(n).value and $ff);
  597. strval:=@strch;
  598. strlength:=1
  599. end
  600. else if is_constresourcestringnode(n) then
  601. begin
  602. strval:=pchar(tconstsym(tloadnode(n).symtableentry).value.valueptr);
  603. strlength:=tconstsym(tloadnode(n).symtableentry).value.len;
  604. end
  605. else
  606. begin
  607. Message(parser_e_illegal_expression);
  608. strlength:=-1;
  609. end;
  610. if strlength>=0 then
  611. begin
  612. case def.stringtype of
  613. st_shortstring:
  614. begin
  615. if strlength>=def.size then
  616. begin
  617. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  618. strlength:=def.size-1;
  619. end;
  620. list.concat(Tai_const.Create_8bit(strlength));
  621. { this can also handle longer strings }
  622. getmem(ca,strlength+1);
  623. move(strval^,ca^,strlength);
  624. ca[strlength]:=#0;
  625. list.concat(Tai_string.Create_pchar(ca,strlength));
  626. { fillup with spaces if size is shorter }
  627. if def.size>strlength then
  628. begin
  629. getmem(ca,def.size-strlength);
  630. { def.size contains also the leading length, so we }
  631. { we have to subtract one }
  632. fillchar(ca[0],def.size-strlength-1,' ');
  633. ca[def.size-strlength-1]:=#0;
  634. { this can also handle longer strings }
  635. list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  636. end;
  637. end;
  638. st_ansistring:
  639. begin
  640. { an empty ansi string is nil! }
  641. if (strlength=0) then
  642. list.concat(Tai_const.Create_sym(nil))
  643. else
  644. begin
  645. current_asmdata.getdatalabel(ll);
  646. list.concat(Tai_const.Create_sym(ll));
  647. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  648. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  649. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength));
  650. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  651. getmem(ca,strlength+1);
  652. move(strval^,ca^,strlength);
  653. { The terminating #0 to be stored in the .data section (JM) }
  654. ca[strlength]:=#0;
  655. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,strlength+1));
  656. end;
  657. end;
  658. st_widestring:
  659. begin
  660. { an empty ansi string is nil! }
  661. if (strlength=0) then
  662. list.concat(Tai_const.Create_sym(nil))
  663. else
  664. begin
  665. current_asmdata.getdatalabel(ll);
  666. list.concat(Tai_const.Create_sym(ll));
  667. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  668. if tf_winlikewidestring in target_info.flags then
  669. current_asmdata.asmlists[al_const].concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  670. else
  671. begin
  672. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  673. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength*cwidechartype.size));
  674. end;
  675. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  676. for i:=0 to strlength-1 do
  677. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  678. { ending #0 }
  679. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(0))
  680. end;
  681. end;
  682. else
  683. internalerror(200107081);
  684. end;
  685. end;
  686. n.free;
  687. end;
  688. { parse a single constant and add it to the packed const info }
  689. { represented by curval etc (see explanation of bitpackval for }
  690. { what the different parameters mean) }
  691. function parse_single_packed_const(list: tasmlist; def: tdef; var bp: tbitpackedval): boolean;
  692. var
  693. n : tnode;
  694. begin
  695. result:=true;
  696. n:=comp_expr(true);
  697. if (n.nodetype <> ordconstn) or
  698. not equal_defs(n.resultdef,def) and
  699. not is_subequal(n.resultdef,def) then
  700. begin
  701. n.free;
  702. incompatibletypes(n.resultdef,def);
  703. consume_all_until(_SEMICOLON);
  704. result:=false;
  705. exit;
  706. end;
  707. bitpackval(tordconstnode(n).value,bp);
  708. if (bp.curbitoffset>=AIntBits) then
  709. flush_packed_value(list,bp);
  710. n.free;
  711. end;
  712. { parses a packed array constant }
  713. procedure parse_packed_array_def(list: tasmlist; def: tarraydef);
  714. var
  715. i : aint;
  716. bp : tbitpackedval;
  717. begin
  718. if not(def.elementdef.typ in [orddef,enumdef]) then
  719. internalerror(2007022010);
  720. { begin of the array }
  721. consume(_LKLAMMER);
  722. initbitpackval(bp,def.elepackedbitsize);
  723. i:=def.lowrange;
  724. { can't use for-loop, fails when cross-compiling from }
  725. { 32 to 64 bit because i is then 64 bit }
  726. while (i<def.highrange) do
  727. begin
  728. { get next item of the packed array }
  729. if not parse_single_packed_const(list,def.elementdef,bp) then
  730. exit;
  731. consume(_COMMA);
  732. inc(i);
  733. end;
  734. { final item }
  735. if not parse_single_packed_const(list,def.elementdef,bp) then
  736. exit;
  737. { flush final incomplete value if necessary }
  738. if (bp.curbitoffset <> 0) then
  739. flush_packed_value(list,bp);
  740. consume(_RKLAMMER);
  741. end;
  742. procedure parse_arraydef(list:tasmlist;def:tarraydef);
  743. var
  744. n : tnode;
  745. i : longint;
  746. len : aint;
  747. ch : char;
  748. ca : pchar;
  749. begin
  750. { dynamic array nil }
  751. if is_dynamic_array(def) then
  752. begin
  753. { Only allow nil initialization }
  754. consume(_NIL);
  755. list.concat(Tai_const.Create_sym(nil));
  756. end
  757. { packed array constant }
  758. else if is_packed_array(def) and
  759. (def.elepackedbitsize mod 8 <> 0) then
  760. begin
  761. parse_packed_array_def(list,def);
  762. end
  763. { normal array const between brackets }
  764. else if try_to_consume(_LKLAMMER) then
  765. begin
  766. for i:=def.lowrange to def.highrange-1 do
  767. begin
  768. read_typed_const_data(list,def.elementdef);
  769. consume(_COMMA);
  770. end;
  771. read_typed_const_data(list,def.elementdef);
  772. consume(_RKLAMMER);
  773. end
  774. { if array of char then we allow also a string }
  775. else if is_char(def.elementdef) then
  776. begin
  777. n:=comp_expr(true);
  778. if n.nodetype=stringconstn then
  779. begin
  780. len:=tstringconstnode(n).len;
  781. { For tp7 the maximum lentgh can be 255 }
  782. if (m_tp7 in current_settings.modeswitches) and
  783. (len>255) then
  784. len:=255;
  785. ca:=tstringconstnode(n).value_str;
  786. end
  787. else
  788. if is_constcharnode(n) then
  789. begin
  790. ch:=chr(tordconstnode(n).value and $ff);
  791. ca:=@ch;
  792. len:=1;
  793. end
  794. else
  795. begin
  796. Message(parser_e_illegal_expression);
  797. len:=0;
  798. end;
  799. if len>(def.highrange-def.lowrange+1) then
  800. Message(parser_e_string_larger_array);
  801. for i:=def.lowrange to def.highrange do
  802. begin
  803. if i+1-def.lowrange<=len then
  804. begin
  805. list.concat(Tai_const.Create_8bit(byte(ca^)));
  806. inc(ca);
  807. end
  808. else
  809. {Fill the remaining positions with #0.}
  810. list.concat(Tai_const.Create_8bit(0));
  811. end;
  812. n.free;
  813. end
  814. else
  815. begin
  816. { we want the ( }
  817. consume(_LKLAMMER);
  818. end;
  819. end;
  820. procedure parse_procvardef(list:tasmlist;def:tprocvardef);
  821. var
  822. tmpn,n : tnode;
  823. pd : tprocdef;
  824. begin
  825. { Procvars and pointers are no longer compatible. }
  826. { under tp: =nil or =var under fpc: =nil or =@var }
  827. if try_to_consume(_NIL) then
  828. begin
  829. list.concat(Tai_const.Create_sym(nil));
  830. if (po_methodpointer in def.procoptions) then
  831. list.concat(Tai_const.Create_sym(nil));
  832. exit;
  833. end;
  834. { you can't assign a value other than NIL to a typed constant }
  835. { which is a "procedure of object", because this also requires }
  836. { address of an object/class instance, which is not known at }
  837. { compile time (JM) }
  838. if (po_methodpointer in def.procoptions) then
  839. Message(parser_e_no_procvarobj_const);
  840. { parse the rest too, so we can continue with error checking }
  841. getprocvardef:=def;
  842. n:=comp_expr(true);
  843. getprocvardef:=nil;
  844. if codegenerror then
  845. begin
  846. n.free;
  847. exit;
  848. end;
  849. { let type conversion check everything needed }
  850. inserttypeconv(n,def);
  851. if codegenerror then
  852. begin
  853. n.free;
  854. exit;
  855. end;
  856. { remove typeconvs, that will normally insert a lea
  857. instruction which is not necessary for us }
  858. while n.nodetype=typeconvn do
  859. begin
  860. tmpn:=ttypeconvnode(n).left;
  861. ttypeconvnode(n).left:=nil;
  862. n.free;
  863. n:=tmpn;
  864. end;
  865. { remove addrn which we also don't need here }
  866. if n.nodetype=addrn then
  867. begin
  868. tmpn:=taddrnode(n).left;
  869. taddrnode(n).left:=nil;
  870. n.free;
  871. n:=tmpn;
  872. end;
  873. { we now need to have a loadn with a procsym }
  874. if (n.nodetype=loadn) and
  875. (tloadnode(n).symtableentry.typ=procsym) then
  876. begin
  877. pd:=tprocdef(tprocsym(tloadnode(n).symtableentry).ProcdefList[0]);
  878. list.concat(Tai_const.createname(pd.mangledname,0));
  879. end
  880. else
  881. Message(parser_e_illegal_expression);
  882. n.free;
  883. end;
  884. procedure parse_recorddef(list:tasmlist;def:trecorddef);
  885. var
  886. n : tnode;
  887. i,
  888. symidx : longint;
  889. recsym,
  890. srsym : tsym;
  891. hs : string;
  892. sorg,s : TIDString;
  893. tmpguid : tguid;
  894. curroffset,
  895. fillbytes : aint;
  896. bp : tbitpackedval;
  897. error,
  898. is_packed: boolean;
  899. begin
  900. { GUID }
  901. if (def=rec_tguid) and
  902. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  903. begin
  904. n:=comp_expr(true);
  905. inserttypeconv(n,cshortstringtype);
  906. if n.nodetype=stringconstn then
  907. begin
  908. hs:=strpas(tstringconstnode(n).value_str);
  909. if string2guid(hs,tmpguid) then
  910. begin
  911. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  912. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  913. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  914. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  915. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  916. end
  917. else
  918. Message(parser_e_improper_guid_syntax);
  919. end
  920. else
  921. Message(parser_e_illegal_expression);
  922. n.free;
  923. exit;
  924. end;
  925. { bitpacked record? }
  926. is_packed:=is_packed_record_or_object(def);
  927. if (is_packed) then
  928. begin
  929. { loadbitsize = 8, bitpacked records are always padded to }
  930. { a multiple of a byte. packedbitsize will be set separately }
  931. { for each field }
  932. initbitpackval(bp,0);
  933. bp.loadbitsize:=8;
  934. end;
  935. { normal record }
  936. consume(_LKLAMMER);
  937. curroffset:=0;
  938. symidx:=0;
  939. sorg:='';
  940. srsym:=tsym(def.symtable.SymList[symidx]);
  941. recsym := nil;
  942. while token<>_RKLAMMER do
  943. begin
  944. s:=pattern;
  945. sorg:=orgpattern;
  946. consume(_ID);
  947. consume(_COLON);
  948. error := false;
  949. recsym := tsym(def.symtable.Find(s));
  950. if not assigned(recsym) then
  951. begin
  952. Message1(sym_e_illegal_field,sorg);
  953. error := true;
  954. end;
  955. if (not error) and
  956. (not assigned(srsym) or
  957. (s <> srsym.name)) then
  958. { possible variant record (JM) }
  959. begin
  960. { All parts of a variant start at the same offset }
  961. { Also allow jumping from one variant part to another, }
  962. { as long as the offsets match }
  963. if (assigned(srsym) and
  964. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  965. { srsym is not assigned after parsing w2 in the }
  966. { typed const in the next example: }
  967. { type tr = record case byte of }
  968. { 1: (l1,l2: dword); }
  969. { 2: (w1,w2: word); }
  970. { end; }
  971. { const r: tr = (w1:1;w2:1;l2:5); }
  972. (tfieldvarsym(recsym).fieldoffset = curroffset) then
  973. srsym := recsym
  974. { going backwards isn't allowed in any mode }
  975. else if (tfieldvarsym(recsym).fieldoffset<curroffset) then
  976. begin
  977. Message(parser_e_invalid_record_const);
  978. error := true;
  979. end
  980. { Delphi allows you to skip fields }
  981. else if (m_delphi in current_settings.modeswitches) then
  982. begin
  983. Message1(parser_w_skipped_fields_before,sorg);
  984. srsym := recsym;
  985. end
  986. { FPC and TP don't }
  987. else
  988. begin
  989. Message1(parser_e_skipped_fields_before,sorg);
  990. error := true;
  991. end;
  992. end;
  993. if error then
  994. consume_all_until(_SEMICOLON)
  995. else
  996. begin
  997. { if needed fill (alignment) }
  998. if tfieldvarsym(srsym).fieldoffset>curroffset then
  999. begin
  1000. if not(is_packed) then
  1001. fillbytes:=tfieldvarsym(srsym).fieldoffset-curroffset
  1002. else
  1003. begin
  1004. flush_packed_value(list,bp);
  1005. { curoffset is now aligned to the next byte }
  1006. curroffset:=align(curroffset,8);
  1007. { offsets are in bits in this case }
  1008. fillbytes:=(tfieldvarsym(srsym).fieldoffset-curroffset) div 8;
  1009. end;
  1010. for i:=1 to fillbytes do
  1011. list.concat(Tai_const.Create_8bit(0))
  1012. end;
  1013. { new position }
  1014. curroffset:=tfieldvarsym(srsym).fieldoffset;
  1015. if not(is_packed) then
  1016. inc(curroffset,tfieldvarsym(srsym).vardef.size)
  1017. else
  1018. inc(curroffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1019. { read the data }
  1020. if not(is_packed) or
  1021. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1022. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1023. begin
  1024. if is_packed then
  1025. begin
  1026. flush_packed_value(list,bp);
  1027. curroffset:=align(curroffset,8);
  1028. end;
  1029. read_typed_const_data(list,tfieldvarsym(srsym).vardef);
  1030. end
  1031. else
  1032. begin
  1033. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1034. parse_single_packed_const(list,tfieldvarsym(srsym).vardef,bp);
  1035. end;
  1036. { keep previous field for checking whether whole }
  1037. { record was initialized (JM) }
  1038. recsym := srsym;
  1039. { goto next field }
  1040. inc(symidx);
  1041. if symidx<def.symtable.SymList.Count then
  1042. srsym:=tsym(def.symtable.SymList[symidx])
  1043. else
  1044. srsym:=nil;
  1045. if token=_SEMICOLON then
  1046. consume(_SEMICOLON)
  1047. else
  1048. break;
  1049. end;
  1050. end;
  1051. { are there any fields left, but don't complain if there only
  1052. come other variant partsa fter the last initialized field }
  1053. if assigned(srsym) and
  1054. (
  1055. (recsym=nil) or
  1056. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1057. ) then
  1058. Message1(parser_w_skipped_fields_after,sorg);
  1059. if not(is_packed) then
  1060. fillbytes:=def.size-curroffset
  1061. else
  1062. begin
  1063. flush_packed_value(list,bp);
  1064. curroffset:=align(curroffset,8);
  1065. fillbytes:=def.size-(curroffset div 8);
  1066. end;
  1067. for i:=1 to fillbytes do
  1068. list.concat(Tai_const.Create_8bit(0));
  1069. consume(_RKLAMMER);
  1070. end;
  1071. procedure parse_objectdef(list:tasmlist;def:tobjectdef);
  1072. var
  1073. n : tnode;
  1074. i : longint;
  1075. obj : tobjectdef;
  1076. srsym : tsym;
  1077. st : tsymtable;
  1078. curroffset : aint;
  1079. s,sorg : TIDString;
  1080. vmtwritten : boolean;
  1081. begin
  1082. { no support for packed object }
  1083. if is_packed_record_or_object(def) then
  1084. begin
  1085. Message(type_e_no_const_packed_record);
  1086. exit;
  1087. end;
  1088. { only allow nil for class and interface }
  1089. if is_class_or_interface(def) then
  1090. begin
  1091. n:=comp_expr(true);
  1092. if n.nodetype<>niln then
  1093. begin
  1094. Message(parser_e_type_const_not_possible);
  1095. consume_all_until(_SEMICOLON);
  1096. end
  1097. else
  1098. list.concat(Tai_const.Create_sym(nil));
  1099. n.free;
  1100. exit;
  1101. end;
  1102. { for objects we allow it only if it doesn't contain a vmt }
  1103. if (oo_has_vmt in def.objectoptions) and
  1104. (m_fpc in current_settings.modeswitches) then
  1105. begin
  1106. Message(parser_e_type_object_constants);
  1107. exit;
  1108. end;
  1109. consume(_LKLAMMER);
  1110. curroffset:=0;
  1111. vmtwritten:=false;
  1112. while token<>_RKLAMMER do
  1113. begin
  1114. s:=pattern;
  1115. sorg:=orgpattern;
  1116. consume(_ID);
  1117. consume(_COLON);
  1118. srsym:=nil;
  1119. obj:=tobjectdef(def);
  1120. st:=obj.symtable;
  1121. while (srsym=nil) and assigned(st) do
  1122. begin
  1123. srsym:=tsym(st.Find(s));
  1124. if assigned(obj) then
  1125. obj:=obj.childof;
  1126. if assigned(obj) then
  1127. st:=obj.symtable
  1128. else
  1129. st:=nil;
  1130. end;
  1131. if srsym=nil then
  1132. begin
  1133. Message1(sym_e_id_not_found,sorg);
  1134. consume_all_until(_SEMICOLON);
  1135. end
  1136. else
  1137. with tfieldvarsym(srsym) do
  1138. begin
  1139. { check position }
  1140. if fieldoffset<curroffset then
  1141. message(parser_e_invalid_record_const);
  1142. { check in VMT needs to be added for TP mode }
  1143. if not(vmtwritten) and
  1144. not(m_fpc in current_settings.modeswitches) and
  1145. (oo_has_vmt in def.objectoptions) and
  1146. (def.vmt_offset<fieldoffset) then
  1147. begin
  1148. for i:=1 to def.vmt_offset-curroffset do
  1149. list.concat(tai_const.create_8bit(0));
  1150. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1151. { this is more general }
  1152. curroffset:=def.vmt_offset + sizeof(aint);
  1153. vmtwritten:=true;
  1154. end;
  1155. { if needed fill }
  1156. if fieldoffset>curroffset then
  1157. for i:=1 to fieldoffset-curroffset do
  1158. list.concat(Tai_const.Create_8bit(0));
  1159. { new position }
  1160. curroffset:=fieldoffset+vardef.size;
  1161. { read the data }
  1162. read_typed_const_data(list,vardef);
  1163. if not try_to_consume(_SEMICOLON) then
  1164. break;
  1165. end;
  1166. end;
  1167. if not(m_fpc in current_settings.modeswitches) and
  1168. (oo_has_vmt in def.objectoptions) and
  1169. (def.vmt_offset>=curroffset) then
  1170. begin
  1171. for i:=1 to def.vmt_offset-curroffset do
  1172. list.concat(tai_const.create_8bit(0));
  1173. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1174. { this is more general }
  1175. curroffset:=def.vmt_offset + sizeof(aint);
  1176. end;
  1177. for i:=1 to def.size-curroffset do
  1178. list.concat(Tai_const.Create_8bit(0));
  1179. consume(_RKLAMMER);
  1180. end;
  1181. var
  1182. old_block_type : tblock_type;
  1183. begin
  1184. old_block_type:=block_type;
  1185. block_type:=bt_const;
  1186. case def.typ of
  1187. orddef :
  1188. parse_orddef(list,torddef(def));
  1189. floatdef :
  1190. parse_floatdef(list,tfloatdef(def));
  1191. classrefdef :
  1192. parse_classrefdef(list,tclassrefdef(def));
  1193. pointerdef :
  1194. parse_pointerdef(list,tpointerdef(def));
  1195. setdef :
  1196. parse_setdef(list,tsetdef(def));
  1197. enumdef :
  1198. parse_enumdef(list,tenumdef(def));
  1199. stringdef :
  1200. parse_stringdef(list,tstringdef(def));
  1201. arraydef :
  1202. parse_arraydef(list,tarraydef(def));
  1203. procvardef:
  1204. parse_procvardef(list,tprocvardef(def));
  1205. recorddef:
  1206. parse_recorddef(list,trecorddef(def));
  1207. objectdef:
  1208. parse_objectdef(list,tobjectdef(def));
  1209. errordef:
  1210. begin
  1211. { try to consume something useful }
  1212. if token=_LKLAMMER then
  1213. consume_all_until(_RKLAMMER)
  1214. else
  1215. consume_all_until(_SEMICOLON);
  1216. end;
  1217. else
  1218. Message(parser_e_type_const_not_possible);
  1219. end;
  1220. block_type:=old_block_type;
  1221. end;
  1222. {$maxfpuregisters default}
  1223. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  1224. var
  1225. storefilepos : tfileposinfo;
  1226. cursectype : TAsmSectionType;
  1227. C_name : string;
  1228. begin
  1229. { mark the staticvarsym as typedconst }
  1230. include(sym.varoptions,vo_is_typed_const);
  1231. { The variable has a value assigned }
  1232. sym.varstate:=vs_initialised;
  1233. { the variable can't be placed in a register }
  1234. sym.varregable:=vr_none;
  1235. { generate data for typed const }
  1236. storefilepos:=current_filepos;
  1237. current_filepos:=sym.fileinfo;
  1238. if sym.varspez=vs_const then
  1239. cursectype:=sec_rodata
  1240. else
  1241. cursectype:=sec_data;
  1242. maybe_new_object_file(list);
  1243. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  1244. if (sym.owner.symtabletype=globalsymtable) or
  1245. maybe_smartlink_symbol or
  1246. (assigned(current_procinfo) and
  1247. (po_inline in current_procinfo.procdef.procoptions)) or
  1248. DLLSource then
  1249. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1250. else
  1251. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1252. read_typed_const_data(list,sym.vardef);
  1253. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1254. current_filepos:=storefilepos;
  1255. { Parse hints }
  1256. try_consume_hintdirective(sym.symoptions);
  1257. { Support public name directive }
  1258. if try_to_consume(_PUBLIC) then
  1259. begin
  1260. include(sym.varoptions,vo_is_public);
  1261. if try_to_consume(_NAME) then
  1262. C_name:=get_stringconst
  1263. else
  1264. C_name:=sym.realname;
  1265. sym.set_mangledname(C_Name);
  1266. end;
  1267. end;
  1268. end.