ptconst.pas 55 KB

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