ptconst.pas 54 KB

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