ptconst.pas 51 KB

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