ptconst.pas 61 KB

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