ngtcon.pas 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl, Jonas Maebe
  3. Generates code/nodes for typed constant declarations
  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 ngtcon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,constexp,
  22. aasmbase,aasmdata,aasmtai,aasmcnst,
  23. node,nbas,
  24. symconst, symtype, symbase, symdef,symsym;
  25. type
  26. ttypedconstbuilder = class
  27. protected
  28. current_old_block_type : tblock_type;
  29. tcsym: tstaticvarsym;
  30. { this procedure reads typed constants }
  31. procedure read_typed_const_data(def:tdef);
  32. procedure parse_orddef(def: torddef);
  33. procedure parse_floatdef(def: tfloatdef);
  34. procedure parse_classrefdef(def: tclassrefdef);
  35. procedure parse_pointerdef(def: tpointerdef);
  36. procedure parse_setdef(def: tsetdef);
  37. procedure parse_enumdef(def: tenumdef);
  38. procedure parse_stringdef(def: tstringdef);
  39. procedure parse_arraydef(def:tarraydef);virtual;abstract;
  40. procedure parse_procvardef(def:tprocvardef);virtual;abstract;
  41. procedure parse_recorddef(def:trecorddef);virtual;abstract;
  42. procedure parse_objectdef(def:tobjectdef);virtual;abstract;
  43. procedure tc_emit_orddef(def: torddef; var node: tnode);virtual;abstract;
  44. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode);virtual;abstract;
  45. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);virtual;abstract;
  46. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);virtual;abstract;
  47. procedure tc_emit_setdef(def: tsetdef; var node: tnode);virtual;abstract;
  48. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);virtual;abstract;
  49. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);virtual;abstract;
  50. public
  51. constructor create(sym: tstaticvarsym);
  52. end;
  53. ttypedconstbuilderclass = class of ttypedconstbuilder;
  54. { should be changed into nested type of tasmlisttypedconstbuilder when
  55. possible }
  56. tbitpackedval = record
  57. curval, nextval: aword;
  58. curbitoffset: smallint;
  59. loadbitsize,packedbitsize: byte;
  60. end;
  61. tasmlisttypedconstbuilder = class(ttypedconstbuilder)
  62. private
  63. fsym: tstaticvarsym;
  64. curoffset: asizeint;
  65. function parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  66. procedure flush_packed_value(var bp: tbitpackedval);
  67. protected
  68. ftcb: ttai_lowleveltypedconstbuilder;
  69. function get_final_asmlist: tasmlist;
  70. procedure parse_packed_array_def(def: tarraydef);
  71. procedure parse_arraydef(def:tarraydef);override;
  72. procedure parse_procvardef(def:tprocvardef);override;
  73. procedure parse_recorddef(def:trecorddef);override;
  74. procedure parse_objectdef(def:tobjectdef);override;
  75. procedure tc_emit_orddef(def: torddef; var node: tnode);override;
  76. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode); override;
  77. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);override;
  78. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);override;
  79. procedure tc_emit_setdef(def: tsetdef; var node: tnode);override;
  80. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);override;
  81. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);override;
  82. procedure tc_emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  83. public
  84. constructor create(sym: tstaticvarsym);virtual;
  85. procedure parse_into_asmlist;
  86. property final_asmlist: tasmlist read get_final_asmlist;
  87. end;
  88. tasmlisttypedconstbuilderclass = class of tasmlisttypedconstbuilder;
  89. tnodetreetypedconstbuilder = class(ttypedconstbuilder)
  90. protected
  91. resultblock: tblocknode;
  92. statmnt: tstatementnode;
  93. { when parsing a record, the base nade becomes a loadnode of the record,
  94. etc. }
  95. basenode: tnode;
  96. procedure parse_arraydef(def:tarraydef);override;
  97. procedure parse_procvardef(def:tprocvardef);override;
  98. procedure parse_recorddef(def:trecorddef);override;
  99. procedure parse_objectdef(def:tobjectdef);override;
  100. procedure tc_emit_orddef(def: torddef; var node: tnode);override;
  101. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode); override;
  102. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);override;
  103. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);override;
  104. procedure tc_emit_setdef(def: tsetdef; var node: tnode);override;
  105. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);override;
  106. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);override;
  107. public
  108. constructor create(sym: tstaticvarsym; previnit: tnode);virtual;
  109. destructor destroy;override;
  110. function parse_into_nodetree: tnode;
  111. end;
  112. tnodetreetypedconstbuilderclass = class of tnodetreetypedconstbuilder;
  113. var
  114. ctypedconstbuilder: ttypedconstbuilderclass;
  115. implementation
  116. uses
  117. SysUtils,
  118. systems,tokens,verbose,
  119. cutils,globals,widestr,scanner,
  120. symtable,
  121. aasmcpu,defutil,defcmp,
  122. { pass 1 }
  123. htypechk,procinfo,
  124. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  125. { parser specific stuff }
  126. pbase,pexpr,pdecvar,
  127. { codegen }
  128. cpuinfo,cgbase,dbgbase,
  129. wpobase
  130. ;
  131. {$maxfpuregisters 0}
  132. function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectList; var symidx:longint):tsym;inline;
  133. begin
  134. while symidx<SymList.Count do
  135. begin
  136. result:=tsym(def.symtable.SymList[symidx]);
  137. inc(symidx);
  138. if result.typ=fieldvarsym then
  139. exit;
  140. end;
  141. result:=nil;
  142. end;
  143. {*****************************************************************************
  144. read typed const
  145. *****************************************************************************}
  146. procedure ttypedconstbuilder.parse_orddef(def:torddef);
  147. var
  148. n : tnode;
  149. begin
  150. n:=comp_expr(true,false);
  151. { for C-style booleans, true=-1 and false=0) }
  152. if is_cbool(def) then
  153. inserttypeconv(n,def);
  154. tc_emit_orddef(def,n);
  155. n.free;
  156. end;
  157. procedure ttypedconstbuilder.parse_floatdef(def:tfloatdef);
  158. var
  159. n : tnode;
  160. begin
  161. n:=comp_expr(true,false);
  162. tc_emit_floatdef(def,n);
  163. n.free;
  164. end;
  165. procedure ttypedconstbuilder.parse_classrefdef(def:tclassrefdef);
  166. var
  167. n : tnode;
  168. begin
  169. n:=comp_expr(true,false);
  170. case n.nodetype of
  171. loadvmtaddrn:
  172. begin
  173. { update wpo info }
  174. if not assigned(current_procinfo) or
  175. (po_inline in current_procinfo.procdef.procoptions) or
  176. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  177. tobjectdef(tclassrefdef(n.resultdef).pointeddef).register_maybe_created_object_type;
  178. end;
  179. end;
  180. tc_emit_classrefdef(def,n);
  181. n.free;
  182. end;
  183. procedure ttypedconstbuilder.parse_pointerdef(def:tpointerdef);
  184. var
  185. p: tnode;
  186. begin
  187. p:=comp_expr(true,false);
  188. tc_emit_pointerdef(def,p);
  189. p.free;
  190. end;
  191. procedure ttypedconstbuilder.parse_setdef(def:tsetdef);
  192. var
  193. p : tnode;
  194. begin
  195. p:=comp_expr(true,false);
  196. tc_emit_setdef(def,p);
  197. p.free;
  198. end;
  199. procedure ttypedconstbuilder.parse_enumdef(def:tenumdef);
  200. var
  201. p : tnode;
  202. begin
  203. p:=comp_expr(true,false);
  204. tc_emit_enumdef(def,p);
  205. p.free;
  206. end;
  207. procedure ttypedconstbuilder.parse_stringdef(def:tstringdef);
  208. var
  209. n : tnode;
  210. begin
  211. n:=comp_expr(true,false);
  212. tc_emit_stringdef(def,n);
  213. n.free;
  214. end;
  215. { ttypedconstbuilder }
  216. procedure ttypedconstbuilder.read_typed_const_data(def:tdef);
  217. var
  218. prev_old_block_type,
  219. old_block_type: tblock_type;
  220. begin
  221. old_block_type:=block_type;
  222. prev_old_block_type:=current_old_block_type;
  223. current_old_block_type:=old_block_type;
  224. block_type:=bt_const;
  225. case def.typ of
  226. orddef :
  227. parse_orddef(torddef(def));
  228. floatdef :
  229. parse_floatdef(tfloatdef(def));
  230. classrefdef :
  231. parse_classrefdef(tclassrefdef(def));
  232. pointerdef :
  233. parse_pointerdef(tpointerdef(def));
  234. setdef :
  235. parse_setdef(tsetdef(def));
  236. enumdef :
  237. parse_enumdef(tenumdef(def));
  238. stringdef :
  239. parse_stringdef(tstringdef(def));
  240. arraydef :
  241. parse_arraydef(tarraydef(def));
  242. procvardef:
  243. parse_procvardef(tprocvardef(def));
  244. recorddef:
  245. parse_recorddef(trecorddef(def));
  246. objectdef:
  247. parse_objectdef(tobjectdef(def));
  248. errordef:
  249. begin
  250. { try to consume something useful }
  251. if token=_LKLAMMER then
  252. consume_all_until(_RKLAMMER)
  253. else
  254. consume_all_until(_SEMICOLON);
  255. end;
  256. else
  257. Message(parser_e_type_const_not_possible);
  258. end;
  259. block_type:=old_block_type;
  260. current_old_block_type:=prev_old_block_type;
  261. end;
  262. constructor ttypedconstbuilder.create(sym: tstaticvarsym);
  263. begin
  264. tcsym:=sym;
  265. end;
  266. {*****************************************************************************
  267. Bitpacked value helpers
  268. *****************************************************************************}
  269. procedure initbitpackval(out bp: tbitpackedval; packedbitsize: byte);
  270. begin
  271. bp.curval:=0;
  272. bp.nextval:=0;
  273. bp.curbitoffset:=0;
  274. bp.packedbitsize:=packedbitsize;
  275. bp.loadbitsize:=packedbitsloadsize(bp.packedbitsize)*8;
  276. end;
  277. {$push}
  278. {$r-}
  279. {$q-}
  280. { (values between quotes below refer to fields of bp; fields not }
  281. { mentioned are unused by this routine) }
  282. { bitpacks "value" as bitpacked value of bitsize "packedbitsize" into }
  283. { "curval", which has already been filled up to "curbitoffset", and }
  284. { stores the spillover if any into "nextval". It also updates }
  285. { curbitoffset to reflect how many bits of currval are now used (can be }
  286. { > AIntBits in case of spillover) }
  287. procedure bitpackval(value: aword; var bp: tbitpackedval);
  288. var
  289. shiftcount: longint;
  290. begin
  291. if (target_info.endian=endian_big) then
  292. begin
  293. { bitpacked format: left-aligned (i.e., "big endian bitness") }
  294. bp.curval:=bp.curval or ((value shl (AIntBits-bp.packedbitsize)) shr bp.curbitoffset);
  295. shiftcount:=((AIntBits-bp.packedbitsize)-bp.curbitoffset);
  296. { carry-over to the next element? }
  297. if (shiftcount<0) then
  298. bp.nextval:=(value and ((aword(1) shl (-shiftcount))-1)) shl
  299. (AIntBits+shiftcount)
  300. end
  301. else
  302. begin
  303. { bitpacked format: right aligned (i.e., "little endian bitness") }
  304. bp.curval:=bp.curval or (value shl bp.curbitoffset);
  305. { carry-over to the next element? }
  306. if (bp.curbitoffset+bp.packedbitsize>AIntBits) then
  307. bp.nextval:=value shr (AIntBits-bp.curbitoffset)
  308. end;
  309. inc(bp.curbitoffset,bp.packedbitsize);
  310. end;
  311. procedure tasmlisttypedconstbuilder.flush_packed_value(var bp: tbitpackedval);
  312. var
  313. bitstowrite: longint;
  314. writeval : AInt;
  315. begin
  316. if (bp.curbitoffset < AIntBits) then
  317. begin
  318. { forced flush -> write multiple of loadsize }
  319. bitstowrite:=align(bp.curbitoffset,bp.loadbitsize);
  320. bp.curbitoffset:=0;
  321. end
  322. else
  323. begin
  324. bitstowrite:=AIntBits;
  325. dec(bp.curbitoffset,AIntBits);
  326. end;
  327. while (bitstowrite>=bp.loadbitsize) do
  328. begin
  329. if (target_info.endian=endian_little) then
  330. begin
  331. { write lowest "loadbitsize" bits }
  332. writeval:=bp.curval and (aint(-1) shr ((sizeof(aint)*8)-bp.loadbitsize));
  333. bp.curval:=bp.curval shr bp.loadbitsize;
  334. end
  335. else
  336. begin
  337. { write highest "loadbitsize" bits }
  338. writeval:=bp.curval shr (AIntBits-bp.loadbitsize);
  339. bp.curval:=bp.curval shl bp.loadbitsize;
  340. end;
  341. case bp.loadbitsize of
  342. 8: ftcb.emit_tai(tai_const.create_8bit(writeval),u8inttype);
  343. 16: ftcb.emit_tai(tai_const.create_16bit(writeval),u16inttype);
  344. 32: ftcb.emit_tai(tai_const.create_32bit(writeval),u32inttype);
  345. 64: ftcb.emit_tai(tai_const.create_64bit(writeval),u64inttype);
  346. else
  347. internalerror(2013111101);
  348. end;
  349. dec(bitstowrite,bp.loadbitsize);
  350. end;
  351. bp.curval:=bp.nextval;
  352. bp.nextval:=0;
  353. end;
  354. {$pop}
  355. { parses a packed array constant }
  356. procedure tasmlisttypedconstbuilder.parse_packed_array_def(def: tarraydef);
  357. var
  358. i : aint;
  359. bp : tbitpackedval;
  360. begin
  361. if not(def.elementdef.typ in [orddef,enumdef]) then
  362. internalerror(2007022010);
  363. { begin of the array }
  364. consume(_LKLAMMER);
  365. initbitpackval(bp,def.elepackedbitsize);
  366. i:=def.lowrange;
  367. { can't use for-loop, fails when cross-compiling from }
  368. { 32 to 64 bit because i is then 64 bit }
  369. while (i<def.highrange) do
  370. begin
  371. { get next item of the packed array }
  372. if not parse_single_packed_const(def.elementdef,bp) then
  373. exit;
  374. consume(_COMMA);
  375. inc(i);
  376. end;
  377. { final item }
  378. if not parse_single_packed_const(def.elementdef,bp) then
  379. exit;
  380. { flush final incomplete value if necessary }
  381. if (bp.curbitoffset <> 0) then
  382. flush_packed_value(bp);
  383. consume(_RKLAMMER);
  384. end;
  385. constructor tasmlisttypedconstbuilder.create(sym: tstaticvarsym);
  386. begin
  387. inherited;
  388. fsym:=sym;
  389. ftcb:=ctai_typedconstbuilder.create;
  390. curoffset:=0;
  391. end;
  392. procedure tasmlisttypedconstbuilder.tc_emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  393. begin
  394. ftcb.emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),charptrdef);
  395. end;
  396. procedure tasmlisttypedconstbuilder.tc_emit_stringdef(def: tstringdef; var node: tnode);
  397. var
  398. strlength : aint;
  399. strval : pchar;
  400. strch : char;
  401. ll : tasmlabofs;
  402. ca : pchar;
  403. winlike : boolean;
  404. hsym : tconstsym;
  405. begin
  406. strval:='';
  407. { load strval and strlength of the constant tree }
  408. if (node.nodetype=stringconstn) or is_wide_or_unicode_string(def) or is_constwidecharnode(node) or
  409. ((node.nodetype=typen) and is_interfacecorba(ttypenode(node).typedef)) then
  410. begin
  411. { convert to the expected string type so that
  412. for widestrings strval is a pcompilerwidestring }
  413. inserttypeconv(node,def);
  414. if (not codegenerror) and
  415. (node.nodetype=stringconstn) then
  416. begin
  417. strlength:=tstringconstnode(node).len;
  418. strval:=tstringconstnode(node).value_str;
  419. { the def may have changed from e.g. RawByteString to
  420. AnsiString(CP_ACP) }
  421. if node.resultdef.typ=stringdef then
  422. def:=tstringdef(node.resultdef)
  423. else
  424. internalerror(2014010501);
  425. end
  426. else
  427. begin
  428. { an error occurred trying to convert the result to a string }
  429. strlength:=-1;
  430. { it's possible that the type conversion could not be
  431. evaluated at compile-time }
  432. if not codegenerror then
  433. CGMessage(parser_e_widestring_to_ansi_compile_time);
  434. end;
  435. end
  436. else if is_constcharnode(node) then
  437. begin
  438. { strval:=pchar(@tordconstnode(node).value);
  439. THIS FAIL on BIG_ENDIAN MACHINES PM }
  440. strch:=chr(tordconstnode(node).value.svalue and $ff);
  441. strval:=@strch;
  442. strlength:=1
  443. end
  444. else if is_constresourcestringnode(node) then
  445. begin
  446. hsym:=tconstsym(tloadnode(node).symtableentry);
  447. strval:=pchar(hsym.value.valueptr);
  448. strlength:=hsym.value.len;
  449. { Delphi-compatible (mis)feature:
  450. Link AnsiString constants to their initializing resourcestring,
  451. enabling them to be (re)translated at runtime.
  452. Wide/UnicodeString are currently rejected above (with incorrect error message).
  453. ShortStrings cannot be handled unless another table is built for them;
  454. considering this acceptable, because Delphi rejects them altogether.
  455. }
  456. if (not is_shortstring(def)) and
  457. ((tcsym.owner.symtablelevel<=main_program_level) or
  458. (current_old_block_type=bt_const)) then
  459. begin
  460. current_asmdata.ResStrInits.Concat(
  461. TTCInitItem.Create(tcsym,curoffset,
  462. current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',hsym.owner,hsym.name),AT_DATA))
  463. );
  464. Include(tcsym.varoptions,vo_force_finalize);
  465. end;
  466. end
  467. else
  468. begin
  469. Message(parser_e_illegal_expression);
  470. strlength:=-1;
  471. end;
  472. if strlength>=0 then
  473. begin
  474. case def.stringtype of
  475. st_shortstring:
  476. begin
  477. ftcb.maybe_begin_aggregate(def);
  478. if strlength>=def.size then
  479. begin
  480. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  481. strlength:=def.size-1;
  482. end;
  483. ftcb.emit_tai(Tai_const.Create_8bit(strlength),cansichartype);
  484. { room for the string data + terminating #0 }
  485. getmem(ca,def.size);
  486. move(strval^,ca^,strlength);
  487. { zero-terminate and fill with spaces if size is shorter }
  488. fillchar(ca[strlength],def.size-strlength-1,' ');
  489. ca[strlength]:=#0;
  490. ca[def.size-1]:=#0;
  491. ftcb.emit_tai(Tai_string.Create_pchar(ca,def.size-1),getarraydef(cansichartype,def.size-1));
  492. ftcb.maybe_end_aggregate(def);
  493. end;
  494. st_ansistring:
  495. begin
  496. { an empty ansi string is nil! }
  497. if (strlength=0) then
  498. begin
  499. ll.lab:=nil;
  500. ll.ofs:=0;
  501. end
  502. else
  503. ll:=ctai_typedconstbuilder.emit_ansistring_const(current_asmdata.asmlists[al_const],strval,strlength,def.encoding,true);
  504. tc_emit_string_offset(ll,strlength,def.stringtype,false,charpointertype);
  505. end;
  506. st_unicodestring,
  507. st_widestring:
  508. begin
  509. { an empty wide/unicode string is nil! }
  510. if (strlength=0) then
  511. begin
  512. ll.lab:=nil;
  513. ll.ofs:=0;
  514. end
  515. else
  516. begin
  517. winlike:=(def.stringtype=st_widestring) and (tf_winlikewidestring in target_info.flags);
  518. ll:=ctai_typedconstbuilder.emit_unicodestring_const(current_asmdata.asmlists[al_const],
  519. strval,
  520. def.encoding,
  521. winlike);
  522. { Collect Windows widestrings that need initialization at startup.
  523. Local initialized vars are excluded because they are initialized
  524. at function entry instead. }
  525. if winlike and
  526. ((tcsym.owner.symtablelevel<=main_program_level) or
  527. (current_old_block_type=bt_const)) then
  528. begin
  529. if ll.ofs<>0 then
  530. internalerror(2012051704);
  531. current_asmdata.WideInits.Concat(
  532. TTCInitItem.Create(tcsym,curoffset,ll.lab)
  533. );
  534. ll.lab:=nil;
  535. ll.ofs:=0;
  536. Include(tcsym.varoptions,vo_force_finalize);
  537. end;
  538. end;
  539. tc_emit_string_offset(ll,strlength,def.stringtype,winlike,widecharpointertype);
  540. end;
  541. else
  542. internalerror(200107081);
  543. end;
  544. end;
  545. end;
  546. procedure tasmlisttypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  547. var
  548. intvalue: tconstexprint;
  549. procedure do_error;
  550. begin
  551. if is_constnode(node) then
  552. IncompatibleTypes(node.resultdef, def)
  553. else if not(parse_generic) then
  554. Message(parser_e_illegal_expression);
  555. end;
  556. begin
  557. case def.ordtype of
  558. pasbool8,
  559. bool8bit :
  560. begin
  561. if is_constboolnode(node) then
  562. begin
  563. testrange(def,tordconstnode(node).value,false,false);
  564. ftcb.emit_tai(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)),def)
  565. end
  566. else
  567. do_error;
  568. end;
  569. pasbool16,
  570. bool16bit :
  571. begin
  572. if is_constboolnode(node) then
  573. begin
  574. testrange(def,tordconstnode(node).value,false,false);
  575. ftcb.emit_tai(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)),def)
  576. end
  577. else
  578. do_error;
  579. end;
  580. pasbool32,
  581. bool32bit :
  582. begin
  583. if is_constboolnode(node) then
  584. begin
  585. testrange(def,tordconstnode(node).value,false,false);
  586. ftcb.emit_tai(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)),def)
  587. end
  588. else
  589. do_error;
  590. end;
  591. pasbool64,
  592. bool64bit :
  593. begin
  594. if is_constboolnode(node) then
  595. begin
  596. testrange(def,tordconstnode(node).value,false,false);
  597. ftcb.emit_tai(Tai_const.Create_64bit(int64(tordconstnode(node).value.svalue)),def)
  598. end
  599. else
  600. do_error;
  601. end;
  602. uchar :
  603. begin
  604. if is_constwidecharnode(node) then
  605. inserttypeconv(node,cansichartype);
  606. if is_constcharnode(node) or
  607. ((m_delphi in current_settings.modeswitches) and
  608. is_constwidecharnode(node) and
  609. (tordconstnode(node).value <= 255)) then
  610. ftcb.emit_tai(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)),def)
  611. else
  612. do_error;
  613. end;
  614. uwidechar :
  615. begin
  616. if is_constcharnode(node) then
  617. inserttypeconv(node,cwidechartype);
  618. if is_constwidecharnode(node) then
  619. ftcb.emit_tai(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)),def)
  620. else
  621. do_error;
  622. end;
  623. s8bit,u8bit,
  624. u16bit,s16bit,
  625. s32bit,u32bit,
  626. s64bit,u64bit :
  627. begin
  628. if is_constintnode(node) then
  629. begin
  630. testrange(def,tordconstnode(node).value,false,false);
  631. case def.size of
  632. 1 :
  633. ftcb.emit_tai(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)),def);
  634. 2 :
  635. ftcb.emit_tai(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)),def);
  636. 4 :
  637. ftcb.emit_tai(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)),def);
  638. 8 :
  639. ftcb.emit_tai(Tai_const.Create_64bit(tordconstnode(node).value.svalue),def);
  640. end;
  641. end
  642. else
  643. do_error;
  644. end;
  645. scurrency:
  646. begin
  647. if is_constintnode(node) then
  648. intvalue:=tordconstnode(node).value*10000
  649. { allow bootstrapping }
  650. else if is_constrealnode(node) then
  651. intvalue:=PInt64(@trealconstnode(node).value_currency)^
  652. else
  653. begin
  654. intvalue:=0;
  655. IncompatibleTypes(node.resultdef, def);
  656. end;
  657. ftcb.emit_tai(Tai_const.Create_64bit(intvalue),def);
  658. end;
  659. else
  660. internalerror(200611052);
  661. end;
  662. end;
  663. procedure tasmlisttypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  664. var
  665. value : bestreal;
  666. begin
  667. value:=0.0;
  668. if is_constrealnode(node) then
  669. value:=trealconstnode(node).value_real
  670. else if is_constintnode(node) then
  671. value:=tordconstnode(node).value
  672. else if is_constnode(node) then
  673. IncompatibleTypes(node.resultdef, def)
  674. else
  675. Message(parser_e_illegal_expression);
  676. case def.floattype of
  677. s32real :
  678. ftcb.emit_tai(tai_realconst.create_s32real(ts32real(value)),def);
  679. s64real :
  680. {$ifdef ARM}
  681. if is_double_hilo_swapped then
  682. ftcb.emit_tai(tai_realconst.create_s64real_hiloswapped(ts64real(value)),def)
  683. else
  684. {$endif ARM}
  685. ftcb.emit_tai(tai_realconst.create_s64real(ts64real(value)),def);
  686. s80real :
  687. ftcb.emit_tai(tai_realconst.create_s80real(value,s80floattype.size),def);
  688. sc80real :
  689. ftcb.emit_tai(tai_realconst.create_s80real(value,sc80floattype.size),def);
  690. s64comp :
  691. { the round is necessary for native compilers where comp isn't a float }
  692. ftcb.emit_tai(tai_realconst.create_s64compreal(round(value)),def);
  693. s64currency:
  694. ftcb.emit_tai(tai_realconst.create_s64compreal(round(value*10000)),def);
  695. s128real:
  696. ftcb.emit_tai(tai_realconst.create_s128real(value),def);
  697. else
  698. internalerror(200611053);
  699. end;
  700. end;
  701. procedure tasmlisttypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; var node: tnode);
  702. begin
  703. case node.nodetype of
  704. loadvmtaddrn:
  705. begin
  706. if not def_is_related(tobjectdef(tclassrefdef(node.resultdef).pointeddef),tobjectdef(def.pointeddef)) then
  707. IncompatibleTypes(node.resultdef, def);
  708. { TODO for correct type? }
  709. ftcb.emit_tai(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(node.resultdef).pointeddef).vmt_mangledname,AT_DATA)),voidpointertype);
  710. end;
  711. niln:
  712. ftcb.emit_tai(Tai_const.Create_sym(nil),voidpointertype);
  713. else if is_constnode(node) then
  714. IncompatibleTypes(node.resultdef, def)
  715. else
  716. Message(parser_e_illegal_expression);
  717. end;
  718. end;
  719. procedure tasmlisttypedconstbuilder.tc_emit_pointerdef(def: tpointerdef; var node: tnode);
  720. var
  721. hp : tnode;
  722. srsym : tsym;
  723. pd : tprocdef;
  724. ca : pchar;
  725. pw : pcompilerwidestring;
  726. i,len : longint;
  727. ll : tasmlabel;
  728. varalign : shortint;
  729. datadef : tdef;
  730. datatcb : ttai_lowleveltypedconstbuilder;
  731. begin
  732. { remove equal typecasts for pointer/nil addresses }
  733. if (node.nodetype=typeconvn) then
  734. with Ttypeconvnode(node) do
  735. if (left.nodetype in [addrn,niln]) and equal_defs(def,node.resultdef) then
  736. begin
  737. hp:=left;
  738. left:=nil;
  739. node.free;
  740. node:=hp;
  741. end;
  742. { allows horrible ofs(typeof(TButton)^) code !! }
  743. if (node.nodetype=addrn) then
  744. with Taddrnode(node) do
  745. if left.nodetype=derefn then
  746. begin
  747. hp:=tderefnode(left).left;
  748. tderefnode(left).left:=nil;
  749. node.free;
  750. node:=hp;
  751. end;
  752. { const pointer ? }
  753. if (node.nodetype = pointerconstn) then
  754. begin
  755. {$if sizeof(TConstPtrUInt)=8}
  756. ftcb.emit_tai(Tai_const.Create_64bit(int64(tpointerconstnode(node).value)),def);
  757. {$else}
  758. {$if sizeof(TConstPtrUInt)=4}
  759. ftcb.emit_tai(Tai_const.Create_32bit(longint(tpointerconstnode(node).value)),def);
  760. {$else}
  761. internalerror(200404122);
  762. {$endif} {$endif}
  763. end
  764. { nil pointer ? }
  765. else if node.nodetype=niln then
  766. ftcb.emit_tai(Tai_const.Create_sym(nil),def)
  767. { maybe pchar ? }
  768. else
  769. if is_char(def.pointeddef) and
  770. (node.nodetype<>addrn) then
  771. begin
  772. { create a tcb for the string data (it's placed in a separate
  773. asmlist) }
  774. datatcb:=ctai_typedconstbuilder.create;
  775. current_asmdata.getlabel(ll,alt_data);
  776. if node.nodetype=stringconstn then
  777. varalign:=size_2_align(tstringconstnode(node).len)
  778. else
  779. varalign:=0;
  780. { represent the string data as an array }
  781. if node.nodetype=stringconstn then
  782. begin
  783. len:=tstringconstnode(node).len;
  784. { For tp7 the maximum lentgh can be 255 }
  785. if (m_tp7 in current_settings.modeswitches) and
  786. (len>255) then
  787. len:=255;
  788. getmem(ca,len+1);
  789. move(tstringconstnode(node).value_str^,ca^,len+1);
  790. datadef:=getarraydef(cansichartype,len+1);
  791. datatcb.maybe_begin_aggregate(datadef);
  792. datatcb.emit_tai(Tai_string.Create_pchar(ca,len+1),datadef);
  793. datatcb.maybe_end_aggregate(datadef);
  794. end
  795. else if is_constcharnode(node) then
  796. begin
  797. datadef:=getarraydef(cansichartype,2);
  798. datatcb.maybe_begin_aggregate(datadef);
  799. datatcb.emit_tai(Tai_string.Create(char(byte(tordconstnode(node).value.svalue))+#0),datadef);
  800. datatcb.maybe_end_aggregate(datadef);
  801. end
  802. else
  803. begin
  804. IncompatibleTypes(node.resultdef, def);
  805. datadef:=getarraydef(cansichartype,1);
  806. end;
  807. current_asmdata.asmlists[al_const].concatlist(datatcb.get_final_asmlist(ll,datadef,sec_rodata,ll.name,varalign,[tcalo_is_lab,tcalo_new_section]));
  808. datatcb.free;
  809. { we now emit the address of the first element of the array
  810. containing the string data }
  811. ftcb.queue_init(def);
  812. { address of ... }
  813. ftcb.queue_addrn(def.pointeddef,def);
  814. { ... the first element ... }
  815. ftcb.queue_vecn(datadef,0);
  816. { ... of the string array }
  817. ftcb.queue_emit_asmsym(ll,datadef);
  818. end
  819. { maybe pwidechar ? }
  820. else
  821. if is_widechar(def.pointeddef) and
  822. (node.nodetype<>addrn) then
  823. begin
  824. if (node.nodetype in [stringconstn,ordconstn]) then
  825. begin
  826. current_asmdata.getlabel(ll,alt_data);
  827. { convert to unicodestring stringconstn }
  828. inserttypeconv(node,cunicodestringtype);
  829. if (node.nodetype=stringconstn) and
  830. (tstringconstnode(node).cst_type in [cst_widestring,cst_unicodestring]) then
  831. begin
  832. { create a tcb for the string data (it's placed in a separate
  833. asmlist) }
  834. datatcb:=ctai_typedconstbuilder.create;
  835. pw:=pcompilerwidestring(tstringconstnode(node).value_str);
  836. { include terminating #0 }
  837. datadef:=getarraydef(cwidechartype,tstringconstnode(node).len+1);
  838. datatcb.maybe_begin_aggregate(datadef);
  839. for i:=0 to tstringconstnode(node).len-1 do
  840. datatcb.emit_tai(Tai_const.Create_16bit(pw^.data[i]),cwidechartype);
  841. { ending #0 }
  842. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  843. datatcb.maybe_end_aggregate(datadef);
  844. { concat add the string data to the al_const asmlist }
  845. current_asmdata.asmlists[al_const].concatlist(datatcb.get_final_asmlist(ll,datadef,sec_rodata,ll.name,const_align(sizeof(pint)),[tcalo_is_lab,tcalo_new_section]));
  846. datatcb.free;
  847. { we now emit the address of the first element of the array
  848. containing the string data }
  849. ftcb.queue_init(def);
  850. { address of ... }
  851. ftcb.queue_addrn(def.pointeddef,def);
  852. { ... the first element ... }
  853. ftcb.queue_vecn(datadef,0);
  854. { ... of the string array }
  855. ftcb.queue_emit_asmsym(ll,datadef);
  856. end;
  857. end
  858. else
  859. IncompatibleTypes(node.resultdef, def);
  860. end
  861. else
  862. if (node.nodetype=addrn) or
  863. is_proc2procvar_load(node,pd) then
  864. begin
  865. { insert typeconv }
  866. inserttypeconv(node,def);
  867. hp:=node;
  868. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  869. hp:=tunarynode(hp).left;
  870. if (hp.nodetype=loadn) then
  871. begin
  872. hp:=node;
  873. ftcb.queue_init(def);
  874. while assigned(hp) and (hp.nodetype<>loadn) do
  875. begin
  876. case hp.nodetype of
  877. vecn :
  878. begin
  879. if is_constintnode(tvecnode(hp).right) then
  880. ftcb.queue_vecn(tvecnode(hp).left.resultdef,get_ordinal_value(tvecnode(hp).right))
  881. else
  882. Message(parser_e_illegal_expression);
  883. end;
  884. subscriptn :
  885. ftcb.queue_subscriptn(tabstractrecorddef(tsubscriptnode(hp).left.resultdef),tsubscriptnode(hp).vs);
  886. typeconvn :
  887. begin
  888. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  889. Message(parser_e_illegal_expression)
  890. else
  891. ftcb.queue_typeconvn(ttypeconvnode(hp).left.resultdef,hp.resultdef);
  892. end;
  893. addrn :
  894. ftcb.queue_addrn(taddrnode(hp).left.resultdef,hp.resultdef);
  895. else
  896. Message(parser_e_illegal_expression);
  897. end;
  898. hp:=tunarynode(hp).left;
  899. end;
  900. srsym:=tloadnode(hp).symtableentry;
  901. case srsym.typ of
  902. procsym :
  903. begin
  904. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  905. if Tprocsym(srsym).ProcdefList.Count>1 then
  906. Message(parser_e_no_overloaded_procvars);
  907. if po_abstractmethod in pd.procoptions then
  908. Message(type_e_cant_take_address_of_abstract_method)
  909. else
  910. ftcb.queue_emit_proc(pd);
  911. end;
  912. staticvarsym :
  913. ftcb.queue_emit_staticvar(tstaticvarsym(srsym));
  914. labelsym :
  915. ftcb.queue_emit_label(tlabelsym(srsym));
  916. constsym :
  917. if tconstsym(srsym).consttyp=constresourcestring then
  918. ftcb.queue_emit_const(tconstsym(srsym))
  919. else
  920. Message(type_e_variable_id_expected);
  921. else
  922. Message(type_e_variable_id_expected);
  923. end;
  924. end
  925. else
  926. Message(parser_e_illegal_expression);
  927. end
  928. else
  929. { allow typeof(Object type)}
  930. if (node.nodetype=inlinen) and
  931. (tinlinenode(node).inlinenumber=in_typeof_x) then
  932. begin
  933. if (tinlinenode(node).left.nodetype=typen) then
  934. begin
  935. // TODO correct type?
  936. ftcb.emit_tai(Tai_const.createname(
  937. tobjectdef(tinlinenode(node).left.resultdef).vmt_mangledname,AT_DATA,0),
  938. voidpointertype);
  939. end
  940. else
  941. Message(parser_e_illegal_expression);
  942. end
  943. else
  944. Message(parser_e_illegal_expression);
  945. end;
  946. procedure tasmlisttypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  947. type
  948. setbytes = array[0..31] of byte;
  949. Psetbytes = ^setbytes;
  950. var
  951. i: longint;
  952. begin
  953. if node.nodetype=setconstn then
  954. begin
  955. { be sure to convert to the correct result, else
  956. it can generate smallset data instead of normalset (PFV) }
  957. inserttypeconv(node,def);
  958. { we only allow const sets }
  959. if (node.nodetype<>setconstn) or
  960. assigned(tsetconstnode(node).left) then
  961. Message(parser_e_illegal_expression)
  962. else
  963. begin
  964. ftcb.maybe_begin_aggregate(def);
  965. tsetconstnode(node).adjustforsetbase;
  966. { this writing is endian-dependant }
  967. if source_info.endian = target_info.endian then
  968. begin
  969. for i:=0 to node.resultdef.size-1 do
  970. ftcb.emit_tai(tai_const.create_8bit(Psetbytes(tsetconstnode(node).value_set)^[i]),u8inttype);
  971. end
  972. else
  973. begin
  974. for i:=0 to node.resultdef.size-1 do
  975. ftcb.emit_tai(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(node).value_set)^[i])),u8inttype);
  976. end;
  977. ftcb.maybe_end_aggregate(def);
  978. end;
  979. end
  980. else
  981. Message(parser_e_illegal_expression);
  982. end;
  983. procedure tasmlisttypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  984. begin
  985. if node.nodetype=ordconstn then
  986. begin
  987. if equal_defs(node.resultdef,def) or
  988. is_subequal(node.resultdef,def) then
  989. begin
  990. testrange(def,tordconstnode(node).value,false,false);
  991. case longint(node.resultdef.size) of
  992. 1 : ftcb.emit_tai(Tai_const.Create_8bit(Byte(tordconstnode(node).value.svalue)),def);
  993. 2 : ftcb.emit_tai(Tai_const.Create_16bit(Word(tordconstnode(node).value.svalue)),def);
  994. 4 : ftcb.emit_tai(Tai_const.Create_32bit(Longint(tordconstnode(node).value.svalue)),def);
  995. end;
  996. end
  997. else
  998. IncompatibleTypes(node.resultdef,def);
  999. end
  1000. else
  1001. Message(parser_e_illegal_expression);
  1002. end;
  1003. { parse a single constant and add it to the packed const info }
  1004. { represented by curval etc (see explanation of bitpackval for }
  1005. { what the different parameters mean) }
  1006. function tasmlisttypedconstbuilder.parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  1007. var
  1008. node: tnode;
  1009. begin
  1010. result:=true;
  1011. node:=comp_expr(true,false);
  1012. if (node.nodetype <> ordconstn) or
  1013. (not equal_defs(node.resultdef,def) and
  1014. not is_subequal(node.resultdef,def)) then
  1015. begin
  1016. node.free;
  1017. incompatibletypes(node.resultdef,def);
  1018. consume_all_until(_SEMICOLON);
  1019. result:=false;
  1020. exit;
  1021. end;
  1022. if (Tordconstnode(node).value<qword(low(Aword))) or (Tordconstnode(node).value>qword(high(Aword))) then
  1023. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(node).value),tostr(low(Aword)),tostr(high(Aword)))
  1024. else
  1025. bitpackval(Tordconstnode(node).value.uvalue,bp);
  1026. if (bp.curbitoffset>=AIntBits) then
  1027. flush_packed_value(bp);
  1028. node.free;
  1029. end;
  1030. function tasmlisttypedconstbuilder.get_final_asmlist: tasmlist;
  1031. var
  1032. asmsym: tasmsymbol;
  1033. addstabx: boolean;
  1034. sec: TAsmSectiontype;
  1035. secname: ansistring;
  1036. begin
  1037. addstabx:=false;
  1038. if fsym.globalasmsym then
  1039. begin
  1040. if (target_dbg.id=dbg_stabx) and
  1041. (cs_debuginfo in current_settings.moduleswitches) and
  1042. not assigned(current_asmdata.GetAsmSymbol(fsym.name)) then
  1043. addstabx:=true;
  1044. asmsym:=current_asmdata.DefineAsmSymbol(fsym.mangledname,AB_GLOBAL,AT_DATA)
  1045. end
  1046. else
  1047. asmsym:=current_asmdata.DefineAsmSymbol(fsym.mangledname,AB_LOCAL,AT_DATA);
  1048. if vo_has_section in fsym.varoptions then
  1049. begin
  1050. sec:=sec_user;
  1051. secname:=fsym.section;
  1052. end
  1053. else
  1054. begin
  1055. if fsym.varspez=vs_const then
  1056. sec:=sec_rodata
  1057. else
  1058. sec:=sec_data;
  1059. secname:=asmsym.Name;
  1060. end;
  1061. result:=ftcb.get_final_asmlist(asmsym,fsym.vardef,sec,secname,fsym.vardef.alignment,[tcalo_new_section]);
  1062. if addstabx then
  1063. begin
  1064. { see same code in ncgutil.insertbssdata }
  1065. result.insert(tai_directive.Create(asd_reference,fsym.name));
  1066. result.insert(tai_symbol.Create(current_asmdata.DefineAsmSymbol(fsym.name,AB_LOCAL,AT_DATA),0));
  1067. end;
  1068. end;
  1069. procedure tasmlisttypedconstbuilder.parse_arraydef(def:tarraydef);
  1070. var
  1071. n : tnode;
  1072. i : longint;
  1073. len : asizeint;
  1074. ch : array[0..1] of char;
  1075. ca : pbyte;
  1076. int_const: tai_const;
  1077. char_size: integer;
  1078. oldoffset: asizeint;
  1079. dummy : byte;
  1080. begin
  1081. { dynamic array nil }
  1082. if is_dynamic_array(def) then
  1083. begin
  1084. { Only allow nil initialization }
  1085. consume(_NIL);
  1086. ftcb.emit_tai(Tai_const.Create_sym(nil),def);
  1087. end
  1088. { packed array constant }
  1089. else if is_packed_array(def) and
  1090. ((def.elepackedbitsize mod 8 <> 0) or
  1091. not ispowerof2(def.elepackedbitsize div 8,i)) then
  1092. begin
  1093. parse_packed_array_def(def);
  1094. end
  1095. { normal array const between brackets }
  1096. else if try_to_consume(_LKLAMMER) then
  1097. begin
  1098. ftcb.maybe_begin_aggregate(def);
  1099. oldoffset:=curoffset;
  1100. curoffset:=0;
  1101. for i:=def.lowrange to def.highrange-1 do
  1102. begin
  1103. read_typed_const_data(def.elementdef);
  1104. Inc(curoffset,def.elementdef.size);
  1105. if token=_RKLAMMER then
  1106. begin
  1107. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1108. consume(_RKLAMMER);
  1109. exit;
  1110. end
  1111. else
  1112. consume(_COMMA);
  1113. end;
  1114. read_typed_const_data(def.elementdef);
  1115. consume(_RKLAMMER);
  1116. curoffset:=oldoffset;
  1117. ftcb.maybe_end_aggregate(def);
  1118. end
  1119. { if array of char then we allow also a string }
  1120. else if is_anychar(def.elementdef) then
  1121. begin
  1122. ftcb.maybe_begin_aggregate(def);
  1123. char_size:=def.elementdef.size;
  1124. n:=comp_expr(true,false);
  1125. if n.nodetype=stringconstn then
  1126. begin
  1127. len:=tstringconstnode(n).len;
  1128. case char_size of
  1129. 1:
  1130. begin
  1131. if (tstringconstnode(n).cst_type in [cst_unicodestring,cst_widestring]) then
  1132. inserttypeconv(n,getansistringdef);
  1133. if n.nodetype<>stringconstn then
  1134. internalerror(2010033003);
  1135. ca:=pointer(tstringconstnode(n).value_str);
  1136. end;
  1137. 2:
  1138. begin
  1139. inserttypeconv(n,cunicodestringtype);
  1140. if n.nodetype<>stringconstn then
  1141. internalerror(2010033003);
  1142. ca:=pointer(pcompilerwidestring(tstringconstnode(n).value_str)^.data)
  1143. end;
  1144. else
  1145. internalerror(2010033005);
  1146. end;
  1147. { For tp7 the maximum lentgh can be 255 }
  1148. if (m_tp7 in current_settings.modeswitches) and
  1149. (len>255) then
  1150. len:=255;
  1151. end
  1152. else if is_constcharnode(n) then
  1153. begin
  1154. case char_size of
  1155. 1:
  1156. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1157. 2:
  1158. begin
  1159. inserttypeconv(n,cwidechartype);
  1160. if not is_constwidecharnode(n) then
  1161. internalerror(2010033001);
  1162. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1163. end;
  1164. else
  1165. internalerror(2010033002);
  1166. end;
  1167. ca:=@ch;
  1168. len:=1;
  1169. end
  1170. else if is_constwidecharnode(n) and (current_settings.sourcecodepage<>CP_UTF8) then
  1171. begin
  1172. case char_size of
  1173. 1:
  1174. begin
  1175. inserttypeconv(n,cansichartype);
  1176. if not is_constcharnode(n) then
  1177. internalerror(2010033001);
  1178. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1179. end;
  1180. 2:
  1181. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1182. else
  1183. internalerror(2010033002);
  1184. end;
  1185. ca:=@ch;
  1186. len:=1;
  1187. end
  1188. else
  1189. begin
  1190. Message(parser_e_illegal_expression);
  1191. len:=0;
  1192. { avoid crash later on }
  1193. dummy:=0;
  1194. ca:=@dummy;
  1195. end;
  1196. if len>(def.highrange-def.lowrange+1) then
  1197. Message(parser_e_string_larger_array);
  1198. for i:=0 to def.highrange-def.lowrange do
  1199. begin
  1200. if i<len then
  1201. begin
  1202. case char_size of
  1203. 1:
  1204. int_const:=Tai_const.Create_char(char_size,pbyte(ca)^);
  1205. 2:
  1206. int_const:=Tai_const.Create_char(char_size,pword(ca)^);
  1207. else
  1208. internalerror(2010033004);
  1209. end;
  1210. inc(ca, char_size);
  1211. end
  1212. else
  1213. {Fill the remaining positions with #0.}
  1214. int_const:=Tai_const.Create_char(char_size,0);
  1215. ftcb.emit_tai(int_const,def.elementdef)
  1216. end;
  1217. ftcb.maybe_end_aggregate(def);
  1218. n.free;
  1219. end
  1220. else
  1221. begin
  1222. { we want the ( }
  1223. consume(_LKLAMMER);
  1224. end;
  1225. end;
  1226. procedure tasmlisttypedconstbuilder.parse_procvardef(def:tprocvardef);
  1227. var
  1228. tmpn,n : tnode;
  1229. pd : tprocdef;
  1230. begin
  1231. { Procvars and pointers are no longer compatible. }
  1232. { under tp: =nil or =var under fpc: =nil or =@var }
  1233. if try_to_consume(_NIL) then
  1234. begin
  1235. ftcb.maybe_begin_aggregate(def);
  1236. { we need the procdef type called by the procvar here, not the
  1237. procvar record }
  1238. ftcb.emit_tai_procvar2procdef(Tai_const.Create_sym(nil),def);
  1239. if not def.is_addressonly then
  1240. ftcb.emit_tai(Tai_const.Create_sym(nil),voidpointertype);
  1241. ftcb.maybe_end_aggregate(def);
  1242. exit;
  1243. end;
  1244. { you can't assign a value other than NIL to a typed constant }
  1245. { which is a "procedure of object", because this also requires }
  1246. { address of an object/class instance, which is not known at }
  1247. { compile time (JM) }
  1248. if (po_methodpointer in def.procoptions) then
  1249. Message(parser_e_no_procvarobj_const);
  1250. { parse the rest too, so we can continue with error checking }
  1251. getprocvardef:=def;
  1252. n:=comp_expr(true,false);
  1253. getprocvardef:=nil;
  1254. if codegenerror then
  1255. begin
  1256. n.free;
  1257. exit;
  1258. end;
  1259. { let type conversion check everything needed }
  1260. inserttypeconv(n,def);
  1261. if codegenerror then
  1262. begin
  1263. n.free;
  1264. exit;
  1265. end;
  1266. { in case of a nested procdef initialised with a global routine }
  1267. ftcb.maybe_begin_aggregate(def);
  1268. { to handle type conversions }
  1269. ftcb.queue_init(def);
  1270. { remove typeconvs, that will normally insert a lea
  1271. instruction which is not necessary for us }
  1272. while n.nodetype=typeconvn do
  1273. begin
  1274. ftcb.queue_typeconvn(ttypeconvnode(n).left.resultdef,n.resultdef);
  1275. tmpn:=ttypeconvnode(n).left;
  1276. ttypeconvnode(n).left:=nil;
  1277. n.free;
  1278. n:=tmpn;
  1279. end;
  1280. { remove addrn which we also don't need here }
  1281. if n.nodetype=addrn then
  1282. begin
  1283. ftcb.queue_addrn(taddrnode(n).left.resultdef,n.resultdef);
  1284. tmpn:=taddrnode(n).left;
  1285. taddrnode(n).left:=nil;
  1286. n.free;
  1287. n:=tmpn;
  1288. end;
  1289. { we now need to have a loadn with a procsym }
  1290. if (n.nodetype=loadn) and
  1291. (tloadnode(n).symtableentry.typ=procsym) then
  1292. begin
  1293. pd:=tloadnode(n).procdef;
  1294. ftcb.queue_emit_proc(pd);
  1295. { nested procvar typed consts can only be initialised with nil
  1296. (checked above) or with a global procedure (checked here),
  1297. because in other cases we need a valid frame pointer }
  1298. if is_nested_pd(def) then
  1299. begin
  1300. if is_nested_pd(pd) then
  1301. Message(parser_e_no_procvarnested_const);
  1302. ftcb.emit_tai(Tai_const.Create_sym(nil),voidpointertype);
  1303. end;
  1304. ftcb.maybe_end_aggregate(def);
  1305. end
  1306. else if n.nodetype=pointerconstn then
  1307. begin
  1308. ftcb.maybe_begin_aggregate(def);
  1309. ftcb.emit_tai_procvar2procdef(Tai_const.Create_pint(tpointerconstnode(n).value),def);
  1310. if not def.is_addressonly then
  1311. ftcb.emit_tai(Tai_const.Create_sym(nil),voidpointertype);
  1312. end
  1313. else
  1314. Message(parser_e_illegal_expression);
  1315. n.free;
  1316. end;
  1317. procedure tasmlisttypedconstbuilder.parse_recorddef(def:trecorddef);
  1318. var
  1319. n : tnode;
  1320. symidx : longint;
  1321. recsym,
  1322. srsym : tsym;
  1323. hs : string;
  1324. sorg,s : TIDString;
  1325. tmpguid : tguid;
  1326. recoffset,
  1327. fillbytes : aint;
  1328. bp : tbitpackedval;
  1329. error,
  1330. is_packed: boolean;
  1331. startoffset: aint;
  1332. procedure handle_stringconstn;
  1333. var
  1334. i : longint;
  1335. begin
  1336. hs:=strpas(tstringconstnode(n).value_str);
  1337. if string2guid(hs,tmpguid) then
  1338. begin
  1339. ftcb.maybe_begin_aggregate(rec_tguid);
  1340. { variant record -> must specify which fields get initialised }
  1341. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  1342. ftcb.emit_tai(Tai_const.Create_32bit(longint(tmpguid.D1)),u32inttype);
  1343. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  1344. ftcb.emit_tai(Tai_const.Create_16bit(tmpguid.D2),u16inttype);
  1345. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  1346. ftcb.emit_tai(Tai_const.Create_16bit(tmpguid.D3),u16inttype);
  1347. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  1348. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1349. ftcb.emit_tai(Tai_const.Create_8bit(tmpguid.D4[i]),u8inttype);
  1350. ftcb.maybe_end_aggregate(rec_tguid);
  1351. end
  1352. else
  1353. Message(parser_e_improper_guid_syntax);
  1354. end;
  1355. var
  1356. i : longint;
  1357. SymList:TFPHashObjectList;
  1358. begin
  1359. { GUID }
  1360. if (def=rec_tguid) and (token=_ID) then
  1361. begin
  1362. n:=comp_expr(true,false);
  1363. if n.nodetype=stringconstn then
  1364. handle_stringconstn
  1365. else
  1366. begin
  1367. inserttypeconv(n,rec_tguid);
  1368. if n.nodetype=guidconstn then
  1369. begin
  1370. ftcb.maybe_begin_aggregate(rec_tguid);
  1371. tmpguid:=tguidconstnode(n).value;
  1372. { variant record -> must specify which fields get initialised }
  1373. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  1374. ftcb.emit_tai(Tai_const.Create_32bit(longint(tmpguid.D1)),u32inttype);
  1375. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  1376. ftcb.emit_tai(Tai_const.Create_16bit(tmpguid.D2),u16inttype);
  1377. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  1378. ftcb.emit_tai(Tai_const.Create_16bit(tmpguid.D3),u16inttype);
  1379. ftcb.next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  1380. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1381. ftcb.emit_tai(Tai_const.Create_8bit(tmpguid.D4[i]),u8inttype);
  1382. ftcb.maybe_end_aggregate(rec_tguid);
  1383. end
  1384. else
  1385. Message(parser_e_illegal_expression);
  1386. end;
  1387. n.free;
  1388. exit;
  1389. end;
  1390. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1391. begin
  1392. n:=comp_expr(true,false);
  1393. inserttypeconv(n,cshortstringtype);
  1394. if n.nodetype=stringconstn then
  1395. handle_stringconstn
  1396. else
  1397. Message(parser_e_illegal_expression);
  1398. n.free;
  1399. exit;
  1400. end;
  1401. ftcb.maybe_begin_aggregate(def);
  1402. { bitpacked record? }
  1403. is_packed:=is_packed_record_or_object(def);
  1404. if (is_packed) then
  1405. begin
  1406. { loadbitsize = 8, bitpacked records are always padded to }
  1407. { a multiple of a byte. packedbitsize will be set separately }
  1408. { for each field }
  1409. initbitpackval(bp,0);
  1410. bp.loadbitsize:=8;
  1411. end;
  1412. { normal record }
  1413. consume(_LKLAMMER);
  1414. recoffset:=0;
  1415. sorg:='';
  1416. symidx:=0;
  1417. symlist:=def.symtable.SymList;
  1418. srsym:=get_next_varsym(def,symlist,symidx);
  1419. recsym := nil;
  1420. startoffset:=curoffset;
  1421. while token<>_RKLAMMER do
  1422. begin
  1423. s:=pattern;
  1424. sorg:=orgpattern;
  1425. consume(_ID);
  1426. consume(_COLON);
  1427. error := false;
  1428. recsym := tsym(def.symtable.Find(s));
  1429. if not assigned(recsym) then
  1430. begin
  1431. Message1(sym_e_illegal_field,sorg);
  1432. error := true;
  1433. end;
  1434. if (not error) and
  1435. (not assigned(srsym) or
  1436. (s <> srsym.name)) then
  1437. { possible variant record (JM) }
  1438. begin
  1439. { All parts of a variant start at the same offset }
  1440. { Also allow jumping from one variant part to another, }
  1441. { as long as the offsets match }
  1442. if (assigned(srsym) and
  1443. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1444. { srsym is not assigned after parsing w2 in the }
  1445. { typed const in the next example: }
  1446. { type tr = record case byte of }
  1447. { 1: (l1,l2: dword); }
  1448. { 2: (w1,w2: word); }
  1449. { end; }
  1450. { const r: tr = (w1:1;w2:1;l2:5); }
  1451. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1452. begin
  1453. srsym:=recsym;
  1454. { symidx should contain the next symbol id to search }
  1455. symidx:=SymList.indexof(srsym)+1;
  1456. end
  1457. { going backwards isn't allowed in any mode }
  1458. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1459. begin
  1460. Message(parser_e_invalid_record_const);
  1461. error := true;
  1462. end
  1463. { Delphi allows you to skip fields }
  1464. else if (m_delphi in current_settings.modeswitches) then
  1465. begin
  1466. Message1(parser_w_skipped_fields_before,sorg);
  1467. srsym := recsym;
  1468. end
  1469. { FPC and TP don't }
  1470. else
  1471. begin
  1472. Message1(parser_e_skipped_fields_before,sorg);
  1473. error := true;
  1474. end;
  1475. end;
  1476. if error then
  1477. consume_all_until(_SEMICOLON)
  1478. else
  1479. begin
  1480. { if needed fill (alignment) }
  1481. if tfieldvarsym(srsym).fieldoffset>recoffset then
  1482. begin
  1483. if not(is_packed) then
  1484. fillbytes:=0
  1485. else
  1486. begin
  1487. flush_packed_value(bp);
  1488. { curoffset is now aligned to the next byte }
  1489. recoffset:=align(recoffset,8);
  1490. { offsets are in bits in this case }
  1491. fillbytes:=(tfieldvarsym(srsym).fieldoffset-recoffset) div 8;
  1492. end;
  1493. for i:=1 to fillbytes do
  1494. ftcb.emit_tai(Tai_const.Create_8bit(0),u8inttype)
  1495. end;
  1496. { new position }
  1497. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1498. if not(is_packed) then
  1499. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1500. else
  1501. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1502. { read the data }
  1503. ftcb.next_field:=tfieldvarsym(srsym);
  1504. if not(is_packed) or
  1505. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1506. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1507. begin
  1508. if is_packed then
  1509. begin
  1510. flush_packed_value(bp);
  1511. recoffset:=align(recoffset,8);
  1512. end;
  1513. curoffset:=startoffset+tfieldvarsym(srsym).fieldoffset;
  1514. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1515. end
  1516. else
  1517. begin
  1518. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1519. parse_single_packed_const(tfieldvarsym(srsym).vardef,bp);
  1520. end;
  1521. { keep previous field for checking whether whole }
  1522. { record was initialized (JM) }
  1523. recsym := srsym;
  1524. { goto next field }
  1525. srsym:=get_next_varsym(def,SymList,symidx);
  1526. if token=_SEMICOLON then
  1527. consume(_SEMICOLON)
  1528. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1529. consume(_COMMA)
  1530. else
  1531. break;
  1532. end;
  1533. end;
  1534. curoffset:=startoffset;
  1535. { are there any fields left, but don't complain if there only
  1536. come other variant parts after the last initialized field }
  1537. if assigned(srsym) and
  1538. (
  1539. (recsym=nil) or
  1540. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1541. ) then
  1542. Message1(parser_w_skipped_fields_after,sorg);
  1543. if not error then
  1544. begin
  1545. if not(is_packed) then
  1546. fillbytes:=0
  1547. else
  1548. begin
  1549. flush_packed_value(bp);
  1550. recoffset:=align(recoffset,8);
  1551. fillbytes:=def.size-(recoffset div 8);
  1552. end;
  1553. for i:=1 to fillbytes do
  1554. ftcb.emit_tai(Tai_const.Create_8bit(0),u8inttype);
  1555. end;
  1556. ftcb.maybe_end_aggregate(def);
  1557. consume(_RKLAMMER);
  1558. end;
  1559. procedure tasmlisttypedconstbuilder.parse_objectdef(def:tobjectdef);
  1560. var
  1561. n : tnode;
  1562. i : longint;
  1563. obj : tobjectdef;
  1564. srsym : tsym;
  1565. st : tsymtable;
  1566. objoffset : aint;
  1567. s,sorg : TIDString;
  1568. vmtwritten : boolean;
  1569. startoffset:aint;
  1570. begin
  1571. { no support for packed object }
  1572. if is_packed_record_or_object(def) then
  1573. begin
  1574. Message(type_e_no_const_packed_record);
  1575. exit;
  1576. end;
  1577. { only allow nil for implicit pointer object types }
  1578. if is_implicit_pointer_object_type(def) then
  1579. begin
  1580. n:=comp_expr(true,false);
  1581. if n.nodetype<>niln then
  1582. begin
  1583. Message(parser_e_type_const_not_possible);
  1584. consume_all_until(_SEMICOLON);
  1585. end
  1586. else
  1587. ftcb.emit_tai(Tai_const.Create_sym(nil),def);
  1588. n.free;
  1589. exit;
  1590. end;
  1591. { for objects we allow it only if it doesn't contain a vmt }
  1592. if (oo_has_vmt in def.objectoptions) and
  1593. (m_fpc in current_settings.modeswitches) then
  1594. begin
  1595. Message(parser_e_type_object_constants);
  1596. exit;
  1597. end;
  1598. ftcb.maybe_begin_aggregate(def);
  1599. consume(_LKLAMMER);
  1600. startoffset:=curoffset;
  1601. objoffset:=0;
  1602. vmtwritten:=false;
  1603. while token<>_RKLAMMER do
  1604. begin
  1605. s:=pattern;
  1606. sorg:=orgpattern;
  1607. consume(_ID);
  1608. consume(_COLON);
  1609. srsym:=nil;
  1610. obj:=tobjectdef(def);
  1611. st:=obj.symtable;
  1612. while (srsym=nil) and assigned(st) do
  1613. begin
  1614. srsym:=tsym(st.Find(s));
  1615. if assigned(obj) then
  1616. obj:=obj.childof;
  1617. if assigned(obj) then
  1618. st:=obj.symtable
  1619. else
  1620. st:=nil;
  1621. end;
  1622. if (srsym=nil) or
  1623. (srsym.typ<>fieldvarsym) then
  1624. begin
  1625. if (srsym=nil) then
  1626. Message1(sym_e_id_not_found,sorg)
  1627. else
  1628. Message1(sym_e_illegal_field,sorg);
  1629. consume_all_until(_RKLAMMER);
  1630. break;
  1631. end
  1632. else
  1633. with tfieldvarsym(srsym) do
  1634. begin
  1635. { check position }
  1636. if fieldoffset<objoffset then
  1637. message(parser_e_invalid_record_const);
  1638. { check in VMT needs to be added for TP mode }
  1639. if not(vmtwritten) and
  1640. not(m_fpc in current_settings.modeswitches) and
  1641. (oo_has_vmt in def.objectoptions) and
  1642. (def.vmt_offset<fieldoffset) then
  1643. begin
  1644. ftcb.next_field:=tfieldvarsym(def.vmt_field);
  1645. ftcb.emit_tai(tai_const.createname(def.vmt_mangledname,AT_DATA,0),tfieldvarsym(def.vmt_field).vardef);
  1646. objoffset:=def.vmt_offset+tfieldvarsym(def.vmt_field).vardef.size;
  1647. vmtwritten:=true;
  1648. end;
  1649. ftcb.next_field:=tfieldvarsym(srsym);
  1650. { new position }
  1651. objoffset:=fieldoffset+vardef.size;
  1652. { read the data }
  1653. curoffset:=startoffset+fieldoffset;
  1654. read_typed_const_data(vardef);
  1655. if not try_to_consume(_SEMICOLON) then
  1656. break;
  1657. end;
  1658. end;
  1659. curoffset:=startoffset;
  1660. if not(m_fpc in current_settings.modeswitches) and
  1661. (oo_has_vmt in def.objectoptions) and
  1662. (def.vmt_offset>=objoffset) then
  1663. begin
  1664. for i:=1 to def.vmt_offset-objoffset do
  1665. ftcb.emit_tai(tai_const.create_8bit(0),u8inttype);
  1666. // TODO VMT type proper tdef?
  1667. ftcb.emit_tai(tai_const.createname(def.vmt_mangledname,AT_DATA,0),voidpointertype);
  1668. { this is more general }
  1669. objoffset:=def.vmt_offset + sizeof(pint);
  1670. end;
  1671. ftcb.maybe_end_aggregate(def);
  1672. consume(_RKLAMMER);
  1673. end;
  1674. procedure tasmlisttypedconstbuilder.parse_into_asmlist;
  1675. begin
  1676. read_typed_const_data(tcsym.vardef);
  1677. end;
  1678. { tnodetreetypedconstbuilder }
  1679. procedure tnodetreetypedconstbuilder.parse_arraydef(def: tarraydef);
  1680. var
  1681. n : tnode;
  1682. i : longint;
  1683. orgbase: tnode;
  1684. begin
  1685. { dynamic array nil }
  1686. if is_dynamic_array(def) then
  1687. begin
  1688. { Only allow nil initialization }
  1689. consume(_NIL);
  1690. addstatement(statmnt,cassignmentnode.create_internal(basenode,cnilnode.create));
  1691. basenode:=nil;
  1692. end
  1693. { array const between brackets }
  1694. else if try_to_consume(_LKLAMMER) then
  1695. begin
  1696. orgbase:=basenode;
  1697. for i:=def.lowrange to def.highrange-1 do
  1698. begin
  1699. basenode:=cvecnode.create(orgbase.getcopy,ctypeconvnode.create_explicit(genintconstnode(i),tarraydef(def).rangedef));
  1700. read_typed_const_data(def.elementdef);
  1701. if token=_RKLAMMER then
  1702. begin
  1703. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1704. consume(_RKLAMMER);
  1705. exit;
  1706. end
  1707. else
  1708. consume(_COMMA);
  1709. end;
  1710. basenode:=cvecnode.create(orgbase,ctypeconvnode.create_explicit(genintconstnode(def.highrange),tarraydef(def).rangedef));
  1711. read_typed_const_data(def.elementdef);
  1712. consume(_RKLAMMER);
  1713. end
  1714. { if array of char then we allow also a string }
  1715. else if is_anychar(def.elementdef) then
  1716. begin
  1717. n:=comp_expr(true,false);
  1718. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1719. basenode:=nil;
  1720. end
  1721. else
  1722. begin
  1723. { we want the ( }
  1724. consume(_LKLAMMER);
  1725. end;
  1726. end;
  1727. procedure tnodetreetypedconstbuilder.parse_procvardef(def: tprocvardef);
  1728. begin
  1729. addstatement(statmnt,cassignmentnode.create_internal(basenode,comp_expr(true,false)));
  1730. basenode:=nil;
  1731. end;
  1732. procedure tnodetreetypedconstbuilder.parse_recorddef(def: trecorddef);
  1733. var
  1734. n,n2 : tnode;
  1735. SymList:TFPHashObjectList;
  1736. orgbasenode : tnode;
  1737. symidx : longint;
  1738. recsym,
  1739. srsym : tsym;
  1740. sorg,s : TIDString;
  1741. recoffset : aint;
  1742. error,
  1743. is_packed: boolean;
  1744. procedure handle_stringconstn;
  1745. begin
  1746. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1747. basenode:=nil;
  1748. n:=nil;
  1749. end;
  1750. begin
  1751. { GUID }
  1752. if (def=rec_tguid) and (token=_ID) then
  1753. begin
  1754. n:=comp_expr(true,false);
  1755. if n.nodetype=stringconstn then
  1756. handle_stringconstn
  1757. else
  1758. begin
  1759. inserttypeconv(n,rec_tguid);
  1760. if n.nodetype=guidconstn then
  1761. begin
  1762. n2:=cstringconstnode.createstr(guid2string(tguidconstnode(n).value));
  1763. n.free;
  1764. n:=n2;
  1765. handle_stringconstn;
  1766. end
  1767. else
  1768. Message(parser_e_illegal_expression);
  1769. end;
  1770. n.free;
  1771. exit;
  1772. end;
  1773. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1774. begin
  1775. n:=comp_expr(true,false);
  1776. inserttypeconv(n,cshortstringtype);
  1777. if n.nodetype=stringconstn then
  1778. handle_stringconstn
  1779. else
  1780. Message(parser_e_illegal_expression);
  1781. n.free;
  1782. exit;
  1783. end;
  1784. { bitpacked record? }
  1785. is_packed:=is_packed_record_or_object(def);
  1786. { normal record }
  1787. consume(_LKLAMMER);
  1788. recoffset:=0;
  1789. sorg:='';
  1790. symidx:=0;
  1791. symlist:=def.symtable.SymList;
  1792. srsym:=get_next_varsym(def,symlist,symidx);
  1793. recsym := nil;
  1794. orgbasenode:=basenode;
  1795. basenode:=nil;
  1796. while token<>_RKLAMMER do
  1797. begin
  1798. s:=pattern;
  1799. sorg:=orgpattern;
  1800. consume(_ID);
  1801. consume(_COLON);
  1802. error := false;
  1803. recsym := tsym(def.symtable.Find(s));
  1804. if not assigned(recsym) then
  1805. begin
  1806. Message1(sym_e_illegal_field,sorg);
  1807. error := true;
  1808. end;
  1809. if (not error) and
  1810. (not assigned(srsym) or
  1811. (s <> srsym.name)) then
  1812. { possible variant record (JM) }
  1813. begin
  1814. { All parts of a variant start at the same offset }
  1815. { Also allow jumping from one variant part to another, }
  1816. { as long as the offsets match }
  1817. if (assigned(srsym) and
  1818. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1819. { srsym is not assigned after parsing w2 in the }
  1820. { typed const in the next example: }
  1821. { type tr = record case byte of }
  1822. { 1: (l1,l2: dword); }
  1823. { 2: (w1,w2: word); }
  1824. { end; }
  1825. { const r: tr = (w1:1;w2:1;l2:5); }
  1826. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1827. begin
  1828. srsym:=recsym;
  1829. { symidx should contain the next symbol id to search }
  1830. symidx:=SymList.indexof(srsym)+1;
  1831. end
  1832. { going backwards isn't allowed in any mode }
  1833. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1834. begin
  1835. Message(parser_e_invalid_record_const);
  1836. error := true;
  1837. end
  1838. { Delphi allows you to skip fields }
  1839. else if (m_delphi in current_settings.modeswitches) then
  1840. begin
  1841. Message1(parser_w_skipped_fields_before,sorg);
  1842. srsym := recsym;
  1843. end
  1844. { FPC and TP don't }
  1845. else
  1846. begin
  1847. Message1(parser_e_skipped_fields_before,sorg);
  1848. error := true;
  1849. end;
  1850. end;
  1851. if error then
  1852. consume_all_until(_SEMICOLON)
  1853. else
  1854. begin
  1855. { skipping fill bytes happens automatically, since we only
  1856. initialize the defined fields }
  1857. { new position }
  1858. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1859. if not(is_packed) then
  1860. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1861. else
  1862. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1863. { read the data }
  1864. if is_packed and
  1865. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1866. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1867. recoffset:=align(recoffset,8);
  1868. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1869. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1870. { keep previous field for checking whether whole }
  1871. { record was initialized (JM) }
  1872. recsym := srsym;
  1873. { goto next field }
  1874. srsym:=get_next_varsym(def,SymList,symidx);
  1875. if token=_SEMICOLON then
  1876. consume(_SEMICOLON)
  1877. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1878. consume(_COMMA)
  1879. else
  1880. break;
  1881. end;
  1882. end;
  1883. { are there any fields left, but don't complain if there only
  1884. come other variant parts after the last initialized field }
  1885. if assigned(srsym) and
  1886. (
  1887. (recsym=nil) or
  1888. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1889. ) then
  1890. Message1(parser_w_skipped_fields_after,sorg);
  1891. orgbasenode.free;
  1892. basenode:=nil;
  1893. consume(_RKLAMMER);
  1894. end;
  1895. procedure tnodetreetypedconstbuilder.parse_objectdef(def: tobjectdef);
  1896. var
  1897. n,
  1898. orgbasenode : tnode;
  1899. obj : tobjectdef;
  1900. srsym : tsym;
  1901. st : tsymtable;
  1902. objoffset : aint;
  1903. s,sorg : TIDString;
  1904. begin
  1905. { no support for packed object }
  1906. if is_packed_record_or_object(def) then
  1907. begin
  1908. Message(type_e_no_const_packed_record);
  1909. exit;
  1910. end;
  1911. { only allow nil for implicit pointer object types }
  1912. if is_implicit_pointer_object_type(def) then
  1913. begin
  1914. n:=comp_expr(true,false);
  1915. if n.nodetype<>niln then
  1916. begin
  1917. Message(parser_e_type_const_not_possible);
  1918. consume_all_until(_SEMICOLON);
  1919. end
  1920. else
  1921. begin
  1922. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1923. n:=nil;
  1924. basenode:=nil;
  1925. end;
  1926. n.free;
  1927. exit;
  1928. end;
  1929. { for objects we allow it only if it doesn't contain a vmt }
  1930. if (oo_has_vmt in def.objectoptions) and
  1931. (m_fpc in current_settings.modeswitches) then
  1932. begin
  1933. Message(parser_e_type_object_constants);
  1934. exit;
  1935. end;
  1936. consume(_LKLAMMER);
  1937. objoffset:=0;
  1938. orgbasenode:=basenode;
  1939. basenode:=nil;
  1940. while token<>_RKLAMMER do
  1941. begin
  1942. s:=pattern;
  1943. sorg:=orgpattern;
  1944. consume(_ID);
  1945. consume(_COLON);
  1946. srsym:=nil;
  1947. obj:=tobjectdef(def);
  1948. st:=obj.symtable;
  1949. while (srsym=nil) and assigned(st) do
  1950. begin
  1951. srsym:=tsym(st.Find(s));
  1952. if assigned(obj) then
  1953. obj:=obj.childof;
  1954. if assigned(obj) then
  1955. st:=obj.symtable
  1956. else
  1957. st:=nil;
  1958. end;
  1959. if (srsym=nil) or
  1960. (srsym.typ<>fieldvarsym) then
  1961. begin
  1962. if (srsym=nil) then
  1963. Message1(sym_e_id_not_found,sorg)
  1964. else
  1965. Message1(sym_e_illegal_field,sorg);
  1966. consume_all_until(_RKLAMMER);
  1967. break;
  1968. end
  1969. else
  1970. with tfieldvarsym(srsym) do
  1971. begin
  1972. { check position }
  1973. if fieldoffset<objoffset then
  1974. message(parser_e_invalid_record_const);
  1975. { new position }
  1976. objoffset:=fieldoffset+vardef.size;
  1977. { read the data }
  1978. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1979. read_typed_const_data(vardef);
  1980. if not try_to_consume(_SEMICOLON) then
  1981. break;
  1982. end;
  1983. end;
  1984. consume(_RKLAMMER);
  1985. end;
  1986. procedure tnodetreetypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  1987. begin
  1988. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1989. basenode:=nil;
  1990. node:=nil;
  1991. end;
  1992. procedure tnodetreetypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  1993. begin
  1994. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1995. basenode:=nil;
  1996. node:=nil;
  1997. end;
  1998. procedure tnodetreetypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; var node: tnode);
  1999. begin
  2000. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  2001. basenode:=nil;
  2002. node:=nil;
  2003. end;
  2004. procedure tnodetreetypedconstbuilder.tc_emit_pointerdef(def: tpointerdef; var node: tnode);
  2005. begin
  2006. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  2007. basenode:=nil;
  2008. node:=nil;
  2009. end;
  2010. procedure tnodetreetypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  2011. begin
  2012. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  2013. basenode:=nil;
  2014. node:=nil;
  2015. end;
  2016. procedure tnodetreetypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  2017. begin
  2018. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  2019. basenode:=nil;
  2020. node:=nil;
  2021. end;
  2022. procedure tnodetreetypedconstbuilder.tc_emit_stringdef(def: tstringdef; var node: tnode);
  2023. begin
  2024. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  2025. basenode:=nil;
  2026. node:=nil;
  2027. end;
  2028. constructor tnodetreetypedconstbuilder.create(sym: tstaticvarsym; previnit: tnode);
  2029. begin
  2030. inherited create(sym);
  2031. basenode:=cloadnode.create(sym,sym.owner);
  2032. resultblock:=internalstatements(statmnt);
  2033. if assigned(previnit) then
  2034. addstatement(statmnt,previnit);
  2035. end;
  2036. destructor tnodetreetypedconstbuilder.destroy;
  2037. begin
  2038. freeandnil(basenode);
  2039. freeandnil(resultblock);
  2040. inherited destroy;
  2041. end;
  2042. function tnodetreetypedconstbuilder.parse_into_nodetree: tnode;
  2043. begin
  2044. read_typed_const_data(tcsym.vardef);
  2045. result:=self.resultblock;
  2046. self.resultblock:=nil;
  2047. end;
  2048. begin
  2049. { default to asmlist version, best for most targets }
  2050. ctypedconstbuilder:=tasmlisttypedconstbuilder;
  2051. end.