ptconst.pas 55 KB

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