ptconst.pas 51 KB

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