ngtcon.pas 88 KB

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