ngtcon.pas 81 KB

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