ptconst.pas 54 KB

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