ptconst.pas 51 KB

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