ptconst.pas 54 KB

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