ptconst.pas 54 KB

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