ptconst.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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,j : 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. { this writing is endian independant }
  532. { untrue - because they are considered }
  533. { arrays of 32-bit values CEC }
  534. if source_info.endian = target_info.endian then
  535. begin
  536. for i:=0 to p.resultdef.size-1 do
  537. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[i]));
  538. end
  539. else
  540. begin
  541. { store as longint values in swaped format }
  542. j:=0;
  543. for i:=0 to ((p.resultdef.size-1) div 4) do
  544. begin
  545. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  546. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  547. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  548. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  549. Inc(j,4);
  550. end;
  551. end;
  552. end;
  553. end
  554. else
  555. Message(parser_e_illegal_expression);
  556. p.free;
  557. end;
  558. procedure parse_enumdef(list:tasmlist;def:tenumdef);
  559. var
  560. p : tnode;
  561. begin
  562. p:=comp_expr(true);
  563. if p.nodetype=ordconstn then
  564. begin
  565. if equal_defs(p.resultdef,def) or
  566. is_subequal(p.resultdef,def) then
  567. begin
  568. case longint(p.resultdef.size) of
  569. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  570. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  571. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  572. end;
  573. end
  574. else
  575. IncompatibleTypes(p.resultdef,def);
  576. end
  577. else
  578. Message(parser_e_illegal_expression);
  579. p.free;
  580. end;
  581. procedure parse_stringdef(list:tasmlist;def:tstringdef);
  582. var
  583. n : tnode;
  584. i : longint;
  585. strlength : aint;
  586. strval : pchar;
  587. strch : char;
  588. ll : tasmlabel;
  589. ca : pchar;
  590. begin
  591. n:=comp_expr(true);
  592. { load strval and strlength of the constant tree }
  593. if (n.nodetype=stringconstn) or is_widestring(def) then
  594. begin
  595. { convert to the expected string type so that
  596. for widestrings strval is a pcompilerwidestring }
  597. inserttypeconv(n,def);
  598. strlength:=tstringconstnode(n).len;
  599. strval:=tstringconstnode(n).value_str;
  600. end
  601. else if is_constcharnode(n) then
  602. begin
  603. { strval:=pchar(@tordconstnode(n).value);
  604. THIS FAIL on BIG_ENDIAN MACHINES PM }
  605. strch:=chr(tordconstnode(n).value and $ff);
  606. strval:=@strch;
  607. strlength:=1
  608. end
  609. else if is_constresourcestringnode(n) then
  610. begin
  611. strval:=pchar(tconstsym(tloadnode(n).symtableentry).value.valueptr);
  612. strlength:=tconstsym(tloadnode(n).symtableentry).value.len;
  613. end
  614. else
  615. begin
  616. Message(parser_e_illegal_expression);
  617. strlength:=-1;
  618. end;
  619. if strlength>=0 then
  620. begin
  621. case def.stringtype of
  622. st_shortstring:
  623. begin
  624. if strlength>=def.size then
  625. begin
  626. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  627. strlength:=def.size-1;
  628. end;
  629. list.concat(Tai_const.Create_8bit(strlength));
  630. { this can also handle longer strings }
  631. getmem(ca,strlength+1);
  632. move(strval^,ca^,strlength);
  633. ca[strlength]:=#0;
  634. list.concat(Tai_string.Create_pchar(ca,strlength));
  635. { fillup with spaces if size is shorter }
  636. if def.size>strlength then
  637. begin
  638. getmem(ca,def.size-strlength);
  639. { def.size contains also the leading length, so we }
  640. { we have to subtract one }
  641. fillchar(ca[0],def.size-strlength-1,' ');
  642. ca[def.size-strlength-1]:=#0;
  643. { this can also handle longer strings }
  644. list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  645. end;
  646. end;
  647. st_ansistring:
  648. begin
  649. { an empty ansi string is nil! }
  650. if (strlength=0) then
  651. list.concat(Tai_const.Create_sym(nil))
  652. else
  653. begin
  654. current_asmdata.getdatalabel(ll);
  655. list.concat(Tai_const.Create_sym(ll));
  656. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  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. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  660. getmem(ca,strlength+1);
  661. move(strval^,ca^,strlength);
  662. { The terminating #0 to be stored in the .data section (JM) }
  663. ca[strlength]:=#0;
  664. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,strlength+1));
  665. end;
  666. end;
  667. st_widestring:
  668. begin
  669. { an empty ansi string is nil! }
  670. if (strlength=0) then
  671. list.concat(Tai_const.Create_sym(nil))
  672. else
  673. begin
  674. current_asmdata.getdatalabel(ll);
  675. list.concat(Tai_const.Create_sym(ll));
  676. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  677. if tf_winlikewidestring in target_info.flags then
  678. current_asmdata.asmlists[al_const].concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  679. else
  680. begin
  681. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  682. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength*cwidechartype.size));
  683. end;
  684. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  685. for i:=0 to strlength-1 do
  686. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  687. { ending #0 }
  688. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(0))
  689. end;
  690. end;
  691. else
  692. internalerror(200107081);
  693. end;
  694. end;
  695. n.free;
  696. end;
  697. { parse a single constant and add it to the packed const info }
  698. { represented by curval etc (see explanation of bitpackval for }
  699. { what the different parameters mean) }
  700. function parse_single_packed_const(list: tasmlist; def: tdef; var bp: tbitpackedval): boolean;
  701. var
  702. n : tnode;
  703. begin
  704. result:=true;
  705. n:=comp_expr(true);
  706. if (n.nodetype <> ordconstn) or
  707. not equal_defs(n.resultdef,def) and
  708. not is_subequal(n.resultdef,def) then
  709. begin
  710. n.free;
  711. incompatibletypes(n.resultdef,def);
  712. consume_all_until(_SEMICOLON);
  713. result:=false;
  714. exit;
  715. end;
  716. bitpackval(tordconstnode(n).value,bp);
  717. if (bp.curbitoffset>=AIntBits) then
  718. flush_packed_value(list,bp);
  719. n.free;
  720. end;
  721. { parses a packed array constant }
  722. procedure parse_packed_array_def(list: tasmlist; def: tarraydef);
  723. var
  724. i : aint;
  725. bp : tbitpackedval;
  726. begin
  727. if not(def.elementdef.typ in [orddef,enumdef]) then
  728. internalerror(2007022010);
  729. { begin of the array }
  730. consume(_LKLAMMER);
  731. initbitpackval(bp,def.elepackedbitsize);
  732. i:=def.lowrange;
  733. { can't use for-loop, fails when cross-compiling from }
  734. { 32 to 64 bit because i is then 64 bit }
  735. while (i<def.highrange) do
  736. begin
  737. { get next item of the packed array }
  738. if not parse_single_packed_const(list,def.elementdef,bp) then
  739. exit;
  740. consume(_COMMA);
  741. inc(i);
  742. end;
  743. { final item }
  744. if not parse_single_packed_const(list,def.elementdef,bp) then
  745. exit;
  746. { flush final incomplete value if necessary }
  747. if (bp.curbitoffset <> 0) then
  748. flush_packed_value(list,bp);
  749. consume(_RKLAMMER);
  750. end;
  751. procedure parse_arraydef(list:tasmlist;def:tarraydef);
  752. var
  753. n : tnode;
  754. i : longint;
  755. len : aint;
  756. ch : char;
  757. ca : pchar;
  758. begin
  759. { dynamic array nil }
  760. if is_dynamic_array(def) then
  761. begin
  762. { Only allow nil initialization }
  763. consume(_NIL);
  764. list.concat(Tai_const.Create_sym(nil));
  765. end
  766. { packed array constant }
  767. else if is_packed_array(def) and
  768. (def.elepackedbitsize mod 8 <> 0) then
  769. begin
  770. parse_packed_array_def(list,def);
  771. end
  772. { normal array const between brackets }
  773. else if try_to_consume(_LKLAMMER) then
  774. begin
  775. for i:=def.lowrange to def.highrange-1 do
  776. begin
  777. read_typed_const_data(list,def.elementdef);
  778. consume(_COMMA);
  779. end;
  780. read_typed_const_data(list,def.elementdef);
  781. consume(_RKLAMMER);
  782. end
  783. { if array of char then we allow also a string }
  784. else if is_char(def.elementdef) then
  785. begin
  786. n:=comp_expr(true);
  787. if n.nodetype=stringconstn then
  788. begin
  789. len:=tstringconstnode(n).len;
  790. { For tp7 the maximum lentgh can be 255 }
  791. if (m_tp7 in current_settings.modeswitches) and
  792. (len>255) then
  793. len:=255;
  794. ca:=tstringconstnode(n).value_str;
  795. end
  796. else
  797. if is_constcharnode(n) then
  798. begin
  799. ch:=chr(tordconstnode(n).value and $ff);
  800. ca:=@ch;
  801. len:=1;
  802. end
  803. else
  804. begin
  805. Message(parser_e_illegal_expression);
  806. len:=0;
  807. end;
  808. if len>(def.highrange-def.lowrange+1) then
  809. Message(parser_e_string_larger_array);
  810. for i:=def.lowrange to def.highrange do
  811. begin
  812. if i+1-def.lowrange<=len then
  813. begin
  814. list.concat(Tai_const.Create_8bit(byte(ca^)));
  815. inc(ca);
  816. end
  817. else
  818. {Fill the remaining positions with #0.}
  819. list.concat(Tai_const.Create_8bit(0));
  820. end;
  821. n.free;
  822. end
  823. else
  824. begin
  825. { we want the ( }
  826. consume(_LKLAMMER);
  827. end;
  828. end;
  829. procedure parse_procvardef(list:tasmlist;def:tprocvardef);
  830. var
  831. tmpn,n : tnode;
  832. pd : tprocdef;
  833. begin
  834. { Procvars and pointers are no longer compatible. }
  835. { under tp: =nil or =var under fpc: =nil or =@var }
  836. if try_to_consume(_NIL) then
  837. begin
  838. list.concat(Tai_const.Create_sym(nil));
  839. if (po_methodpointer in def.procoptions) then
  840. list.concat(Tai_const.Create_sym(nil));
  841. exit;
  842. end;
  843. { you can't assign a value other than NIL to a typed constant }
  844. { which is a "procedure of object", because this also requires }
  845. { address of an object/class instance, which is not known at }
  846. { compile time (JM) }
  847. if (po_methodpointer in def.procoptions) then
  848. Message(parser_e_no_procvarobj_const);
  849. { parse the rest too, so we can continue with error checking }
  850. getprocvardef:=def;
  851. n:=comp_expr(true);
  852. getprocvardef:=nil;
  853. if codegenerror then
  854. begin
  855. n.free;
  856. exit;
  857. end;
  858. { let type conversion check everything needed }
  859. inserttypeconv(n,def);
  860. if codegenerror then
  861. begin
  862. n.free;
  863. exit;
  864. end;
  865. { remove typeconvs, that will normally insert a lea
  866. instruction which is not necessary for us }
  867. while n.nodetype=typeconvn do
  868. begin
  869. tmpn:=ttypeconvnode(n).left;
  870. ttypeconvnode(n).left:=nil;
  871. n.free;
  872. n:=tmpn;
  873. end;
  874. { remove addrn which we also don't need here }
  875. if n.nodetype=addrn then
  876. begin
  877. tmpn:=taddrnode(n).left;
  878. taddrnode(n).left:=nil;
  879. n.free;
  880. n:=tmpn;
  881. end;
  882. { we now need to have a loadn with a procsym }
  883. if (n.nodetype=loadn) and
  884. (tloadnode(n).symtableentry.typ=procsym) then
  885. begin
  886. pd:=tprocdef(tprocsym(tloadnode(n).symtableentry).ProcdefList[0]);
  887. list.concat(Tai_const.createname(pd.mangledname,0));
  888. end
  889. else
  890. Message(parser_e_illegal_expression);
  891. n.free;
  892. end;
  893. procedure parse_recorddef(list:tasmlist;def:trecorddef);
  894. var
  895. n : tnode;
  896. i,
  897. symidx : longint;
  898. recsym,
  899. srsym : tsym;
  900. hs : string;
  901. sorg,s : TIDString;
  902. tmpguid : tguid;
  903. curroffset,
  904. fillbytes : aint;
  905. bp : tbitpackedval;
  906. error,
  907. is_packed: boolean;
  908. begin
  909. { GUID }
  910. if (def=rec_tguid) and
  911. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  912. begin
  913. n:=comp_expr(true);
  914. inserttypeconv(n,cshortstringtype);
  915. if n.nodetype=stringconstn then
  916. begin
  917. hs:=strpas(tstringconstnode(n).value_str);
  918. if string2guid(hs,tmpguid) then
  919. begin
  920. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  921. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  922. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  923. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  924. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  925. end
  926. else
  927. Message(parser_e_improper_guid_syntax);
  928. end
  929. else
  930. Message(parser_e_illegal_expression);
  931. n.free;
  932. exit;
  933. end;
  934. { bitpacked record? }
  935. is_packed:=is_packed_record_or_object(def);
  936. if (is_packed) then
  937. begin
  938. { loadbitsize = 8, bitpacked records are always padded to }
  939. { a multiple of a byte. packedbitsize will be set separately }
  940. { for each field }
  941. initbitpackval(bp,0);
  942. bp.loadbitsize:=8;
  943. end;
  944. { normal record }
  945. consume(_LKLAMMER);
  946. curroffset:=0;
  947. symidx:=0;
  948. sorg:='';
  949. srsym:=tsym(def.symtable.SymList[symidx]);
  950. recsym := nil;
  951. while token<>_RKLAMMER do
  952. begin
  953. s:=pattern;
  954. sorg:=orgpattern;
  955. consume(_ID);
  956. consume(_COLON);
  957. error := false;
  958. recsym := tsym(def.symtable.Find(s));
  959. if not assigned(recsym) then
  960. begin
  961. Message1(sym_e_illegal_field,sorg);
  962. error := true;
  963. end;
  964. if (not error) and
  965. (not assigned(srsym) or
  966. (s <> srsym.name)) then
  967. { possible variant record (JM) }
  968. begin
  969. { All parts of a variant start at the same offset }
  970. { Also allow jumping from one variant part to another, }
  971. { as long as the offsets match }
  972. if (assigned(srsym) and
  973. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  974. { srsym is not assigned after parsing w2 in the }
  975. { typed const in the next example: }
  976. { type tr = record case byte of }
  977. { 1: (l1,l2: dword); }
  978. { 2: (w1,w2: word); }
  979. { end; }
  980. { const r: tr = (w1:1;w2:1;l2:5); }
  981. (tfieldvarsym(recsym).fieldoffset = curroffset) then
  982. srsym := recsym
  983. { going backwards isn't allowed in any mode }
  984. else if (tfieldvarsym(recsym).fieldoffset<curroffset) then
  985. begin
  986. Message(parser_e_invalid_record_const);
  987. error := true;
  988. end
  989. { Delphi allows you to skip fields }
  990. else if (m_delphi in current_settings.modeswitches) then
  991. begin
  992. Message1(parser_w_skipped_fields_before,sorg);
  993. srsym := recsym;
  994. end
  995. { FPC and TP don't }
  996. else
  997. begin
  998. Message1(parser_e_skipped_fields_before,sorg);
  999. error := true;
  1000. end;
  1001. end;
  1002. if error then
  1003. consume_all_until(_SEMICOLON)
  1004. else
  1005. begin
  1006. { if needed fill (alignment) }
  1007. if tfieldvarsym(srsym).fieldoffset>curroffset then
  1008. begin
  1009. if not(is_packed) then
  1010. fillbytes:=tfieldvarsym(srsym).fieldoffset-curroffset
  1011. else
  1012. begin
  1013. flush_packed_value(list,bp);
  1014. { curoffset is now aligned to the next byte }
  1015. curroffset:=align(curroffset,8);
  1016. { offsets are in bits in this case }
  1017. fillbytes:=(tfieldvarsym(srsym).fieldoffset-curroffset) div 8;
  1018. end;
  1019. for i:=1 to fillbytes do
  1020. list.concat(Tai_const.Create_8bit(0))
  1021. end;
  1022. { new position }
  1023. curroffset:=tfieldvarsym(srsym).fieldoffset;
  1024. if not(is_packed) then
  1025. inc(curroffset,tfieldvarsym(srsym).vardef.size)
  1026. else
  1027. inc(curroffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1028. { read the data }
  1029. if not(is_packed) or
  1030. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1031. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1032. begin
  1033. if is_packed then
  1034. begin
  1035. flush_packed_value(list,bp);
  1036. curroffset:=align(curroffset,8);
  1037. end;
  1038. read_typed_const_data(list,tfieldvarsym(srsym).vardef);
  1039. end
  1040. else
  1041. begin
  1042. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1043. parse_single_packed_const(list,tfieldvarsym(srsym).vardef,bp);
  1044. end;
  1045. { keep previous field for checking whether whole }
  1046. { record was initialized (JM) }
  1047. recsym := srsym;
  1048. { goto next field }
  1049. inc(symidx);
  1050. if symidx<def.symtable.SymList.Count then
  1051. srsym:=tsym(def.symtable.SymList[symidx])
  1052. else
  1053. srsym:=nil;
  1054. if token=_SEMICOLON then
  1055. consume(_SEMICOLON)
  1056. else
  1057. break;
  1058. end;
  1059. end;
  1060. { are there any fields left, but don't complain if there only
  1061. come other variant partsa fter the last initialized field }
  1062. if assigned(srsym) and
  1063. (
  1064. (recsym=nil) or
  1065. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1066. ) then
  1067. Message1(parser_w_skipped_fields_after,sorg);
  1068. if not(is_packed) then
  1069. fillbytes:=def.size-curroffset
  1070. else
  1071. begin
  1072. flush_packed_value(list,bp);
  1073. curroffset:=align(curroffset,8);
  1074. fillbytes:=def.size-(curroffset div 8);
  1075. end;
  1076. for i:=1 to fillbytes do
  1077. list.concat(Tai_const.Create_8bit(0));
  1078. consume(_RKLAMMER);
  1079. end;
  1080. procedure parse_objectdef(list:tasmlist;def:tobjectdef);
  1081. var
  1082. n : tnode;
  1083. i : longint;
  1084. obj : tobjectdef;
  1085. srsym : tsym;
  1086. st : tsymtable;
  1087. curroffset : aint;
  1088. s,sorg : TIDString;
  1089. vmtwritten : boolean;
  1090. begin
  1091. { no support for packed object }
  1092. if is_packed_record_or_object(def) then
  1093. begin
  1094. Message(type_e_no_const_packed_record);
  1095. exit;
  1096. end;
  1097. { only allow nil for class and interface }
  1098. if is_class_or_interface(def) then
  1099. begin
  1100. n:=comp_expr(true);
  1101. if n.nodetype<>niln then
  1102. begin
  1103. Message(parser_e_type_const_not_possible);
  1104. consume_all_until(_SEMICOLON);
  1105. end
  1106. else
  1107. list.concat(Tai_const.Create_sym(nil));
  1108. n.free;
  1109. exit;
  1110. end;
  1111. { for objects we allow it only if it doesn't contain a vmt }
  1112. if (oo_has_vmt in def.objectoptions) and
  1113. (m_fpc in current_settings.modeswitches) then
  1114. begin
  1115. Message(parser_e_type_object_constants);
  1116. exit;
  1117. end;
  1118. consume(_LKLAMMER);
  1119. curroffset:=0;
  1120. vmtwritten:=false;
  1121. while token<>_RKLAMMER do
  1122. begin
  1123. s:=pattern;
  1124. sorg:=orgpattern;
  1125. consume(_ID);
  1126. consume(_COLON);
  1127. srsym:=nil;
  1128. obj:=tobjectdef(def);
  1129. st:=obj.symtable;
  1130. while (srsym=nil) and assigned(st) do
  1131. begin
  1132. srsym:=tsym(st.Find(s));
  1133. if assigned(obj) then
  1134. obj:=obj.childof;
  1135. if assigned(obj) then
  1136. st:=obj.symtable
  1137. else
  1138. st:=nil;
  1139. end;
  1140. if srsym=nil then
  1141. begin
  1142. Message1(sym_e_id_not_found,sorg);
  1143. consume_all_until(_SEMICOLON);
  1144. end
  1145. else
  1146. with tfieldvarsym(srsym) do
  1147. begin
  1148. { check position }
  1149. if fieldoffset<curroffset then
  1150. message(parser_e_invalid_record_const);
  1151. { check in VMT needs to be added for TP mode }
  1152. if not(vmtwritten) and
  1153. not(m_fpc in current_settings.modeswitches) and
  1154. (oo_has_vmt in def.objectoptions) and
  1155. (def.vmt_offset<fieldoffset) then
  1156. begin
  1157. for i:=1 to def.vmt_offset-curroffset do
  1158. list.concat(tai_const.create_8bit(0));
  1159. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1160. { this is more general }
  1161. curroffset:=def.vmt_offset + sizeof(aint);
  1162. vmtwritten:=true;
  1163. end;
  1164. { if needed fill }
  1165. if fieldoffset>curroffset then
  1166. for i:=1 to fieldoffset-curroffset do
  1167. list.concat(Tai_const.Create_8bit(0));
  1168. { new position }
  1169. curroffset:=fieldoffset+vardef.size;
  1170. { read the data }
  1171. read_typed_const_data(list,vardef);
  1172. if not try_to_consume(_SEMICOLON) then
  1173. break;
  1174. end;
  1175. end;
  1176. if not(m_fpc in current_settings.modeswitches) and
  1177. (oo_has_vmt in def.objectoptions) and
  1178. (def.vmt_offset>=curroffset) then
  1179. begin
  1180. for i:=1 to def.vmt_offset-curroffset do
  1181. list.concat(tai_const.create_8bit(0));
  1182. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1183. { this is more general }
  1184. curroffset:=def.vmt_offset + sizeof(aint);
  1185. end;
  1186. for i:=1 to def.size-curroffset do
  1187. list.concat(Tai_const.Create_8bit(0));
  1188. consume(_RKLAMMER);
  1189. end;
  1190. var
  1191. old_block_type : tblock_type;
  1192. begin
  1193. old_block_type:=block_type;
  1194. block_type:=bt_const;
  1195. case def.typ of
  1196. orddef :
  1197. parse_orddef(list,torddef(def));
  1198. floatdef :
  1199. parse_floatdef(list,tfloatdef(def));
  1200. classrefdef :
  1201. parse_classrefdef(list,tclassrefdef(def));
  1202. pointerdef :
  1203. parse_pointerdef(list,tpointerdef(def));
  1204. setdef :
  1205. parse_setdef(list,tsetdef(def));
  1206. enumdef :
  1207. parse_enumdef(list,tenumdef(def));
  1208. stringdef :
  1209. parse_stringdef(list,tstringdef(def));
  1210. arraydef :
  1211. parse_arraydef(list,tarraydef(def));
  1212. procvardef:
  1213. parse_procvardef(list,tprocvardef(def));
  1214. recorddef:
  1215. parse_recorddef(list,trecorddef(def));
  1216. objectdef:
  1217. parse_objectdef(list,tobjectdef(def));
  1218. errordef:
  1219. begin
  1220. { try to consume something useful }
  1221. if token=_LKLAMMER then
  1222. consume_all_until(_RKLAMMER)
  1223. else
  1224. consume_all_until(_SEMICOLON);
  1225. end;
  1226. else
  1227. Message(parser_e_type_const_not_possible);
  1228. end;
  1229. block_type:=old_block_type;
  1230. end;
  1231. {$maxfpuregisters default}
  1232. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  1233. var
  1234. storefilepos : tfileposinfo;
  1235. cursectype : TAsmSectionType;
  1236. valuelist : tasmlist;
  1237. begin
  1238. { mark the staticvarsym as typedconst }
  1239. include(sym.varoptions,vo_is_typed_const);
  1240. { The variable has a value assigned }
  1241. sym.varstate:=vs_initialised;
  1242. { the variable can't be placed in a register }
  1243. sym.varregable:=vr_none;
  1244. { generate data for typed const }
  1245. storefilepos:=current_filepos;
  1246. current_filepos:=sym.fileinfo;
  1247. if sym.varspez=vs_const then
  1248. cursectype:=sec_rodata
  1249. else
  1250. cursectype:=sec_data;
  1251. maybe_new_object_file(list);
  1252. valuelist:=tasmlist.create;
  1253. read_typed_const_data(valuelist,sym.vardef);
  1254. { Parse hints }
  1255. try_consume_hintdirective(sym.symoptions);
  1256. consume(_SEMICOLON);
  1257. { parse public/external/export/... }
  1258. if (
  1259. (
  1260. (token = _ID) and
  1261. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) and
  1262. (m_cvar_support in current_settings.modeswitches)
  1263. ) or
  1264. (
  1265. (m_mac in current_settings.modeswitches) and
  1266. (
  1267. (cs_external_var in current_settings.localswitches) or
  1268. (cs_externally_visible in current_settings.localswitches)
  1269. )
  1270. )
  1271. ) then
  1272. read_public_and_external(sym);
  1273. { only now add items based on the symbolname, because it may }
  1274. { have been modified by the directives parsed above }
  1275. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  1276. if (sym.owner.symtabletype=globalsymtable) or
  1277. maybe_smartlink_symbol or
  1278. (assigned(current_procinfo) and
  1279. (po_inline in current_procinfo.procdef.procoptions)) or
  1280. DLLSource then
  1281. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1282. else
  1283. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1284. { add the parsed value }
  1285. list.concatlist(valuelist);
  1286. valuelist.free;
  1287. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1288. current_filepos:=storefilepos;
  1289. end;
  1290. end.