ptconst.pas 52 KB

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