ngtcon.pas 79 KB

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