ngtcon.pas 80 KB

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