ptconst.pas 61 KB

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