ptconst.pas 54 KB

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