ptconst.pas 61 KB

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