ptconst.pas 56 KB

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