ngtcon.pas 80 KB

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