ptconst.pas 53 KB

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