ptconst.pas 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads typed constants
  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 ptconst;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses symtype,symsym,aasmdata;
  21. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,systems,tokens,verbose,
  26. cutils,globals,widestr,scanner,
  27. symconst,symbase,symdef,symtable,
  28. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  29. { pass 1 }
  30. node,htypechk,procinfo,
  31. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  32. { parser specific stuff }
  33. pbase,pexpr,
  34. { codegen }
  35. cpuinfo,cgbase,dbgbase
  36. ;
  37. {$maxfpuregisters 0}
  38. { bitpacks "value" as bitpacked value of bitsize "packedbitsize" and }
  39. { loadsize "loadbitsize" into "curval", which has already been filled up }
  40. { to "curbitoffset", and stores the spillover if any into "nextval". }
  41. { It also updates curbitoffset to reflect how many bits of currval are }
  42. { now used (can be > packedbitsize in case of spillover) }
  43. procedure bitpackval(value: aword; var curval: aword; out nextval: aword; loadbitsize, packedbitsize: byte; var curbitoffset: smallint);
  44. var
  45. tmpval: aword;
  46. shiftcount: longint;
  47. begin
  48. { 1 shl 32/64 = 1 on i386/x86_64 }
  49. if (loadbitsize<>AintBits) then
  50. tmpval:=(aword(1) shl loadbitsize) - 1
  51. else
  52. tmpval:=aword(-1);
  53. if (target_info.endian=endian_big) then
  54. begin
  55. { bitpacked format: left-aligned (i.e., "big endian bitness") }
  56. curval:=curval or (((value shl (loadbitsize-packedbitsize)) shr curbitoffset) and tmpval);
  57. shiftcount:=((loadbitsize-packedbitsize)-curbitoffset);
  58. { carry-over to the next element? }
  59. if (shiftcount<0) then
  60. nextval:=(value and ((aword(1) shl (-shiftcount))-1)) shl
  61. (loadbitsize+shiftcount)
  62. else
  63. nextval:=0;
  64. end
  65. else
  66. begin
  67. { bitpacked format: right aligned (i.e., "little endian bitness") }
  68. curval:=curval or ((value shl curbitoffset) and tmpval);
  69. { carry-over to the next element? }
  70. if (curbitoffset+packedbitsize>loadbitsize) then
  71. nextval:=value shr (loadbitsize-curbitoffset)
  72. else
  73. nextval:=0;
  74. end;
  75. inc(curbitoffset,packedbitsize);
  76. end;
  77. { this procedure reads typed constants }
  78. procedure read_typed_const_data(list:tasmlist;def:tdef);
  79. procedure parse_orddef(list:tasmlist;def:torddef);
  80. var
  81. n : tnode;
  82. intvalue : tconstexprint;
  83. begin
  84. n:=comp_expr(true);
  85. case def.ordtype of
  86. bool8bit :
  87. begin
  88. if is_constboolnode(n) then
  89. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value)))
  90. else
  91. IncompatibleTypes(n.resultdef, def);
  92. end;
  93. bool16bit :
  94. begin
  95. if is_constboolnode(n) then
  96. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value)))
  97. else
  98. IncompatibleTypes(n.resultdef, def);
  99. end;
  100. bool32bit :
  101. begin
  102. if is_constboolnode(n) then
  103. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value)))
  104. else
  105. IncompatibleTypes(n.resultdef, def);
  106. end;
  107. bool64bit :
  108. begin
  109. if is_constboolnode(n) then
  110. list.concat(Tai_const.Create_64bit(int64(tordconstnode(n).value)))
  111. else
  112. IncompatibleTypes(n.resultdef, def);
  113. end;
  114. uchar :
  115. begin
  116. if is_constcharnode(n) or
  117. ((m_delphi in current_settings.modeswitches) and
  118. is_constwidecharnode(n) and
  119. (tordconstnode(n).value <= 255)) then
  120. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value)))
  121. else
  122. IncompatibleTypes(n.resultdef, def);
  123. end;
  124. uwidechar :
  125. begin
  126. if is_constcharnode(n) then
  127. inserttypeconv(n,cwidechartype);
  128. if is_constwidecharnode(n) then
  129. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value)))
  130. else
  131. IncompatibleTypes(n.resultdef, def);
  132. end;
  133. s8bit,u8bit,
  134. u16bit,s16bit,
  135. s32bit,u32bit,
  136. s64bit,u64bit :
  137. begin
  138. if is_constintnode(n) then
  139. begin
  140. testrange(def,tordconstnode(n).value,false);
  141. case def.size of
  142. 1 :
  143. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value)));
  144. 2 :
  145. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value)));
  146. 4 :
  147. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value)));
  148. 8 :
  149. list.concat(Tai_const.Create_64bit(tordconstnode(n).value));
  150. end;
  151. end
  152. else
  153. IncompatibleTypes(n.resultdef, def);
  154. end;
  155. scurrency:
  156. begin
  157. if is_constintnode(n) then
  158. intvalue := tordconstnode(n).value
  159. { allow bootstrapping }
  160. else if is_constrealnode(n) then
  161. intvalue:=PInt64(@trealconstnode(n).value_currency)^
  162. else
  163. begin
  164. intvalue:=0;
  165. IncompatibleTypes(n.resultdef, def);
  166. end;
  167. list.concat(Tai_const.Create_64bit(intvalue));
  168. end;
  169. else
  170. internalerror(200611052);
  171. end;
  172. n.free;
  173. end;
  174. procedure parse_floatdef(list:tasmlist;def:tfloatdef);
  175. var
  176. n : tnode;
  177. value : bestreal;
  178. begin
  179. n:=comp_expr(true);
  180. if is_constrealnode(n) then
  181. value:=trealconstnode(n).value_real
  182. else if is_constintnode(n) then
  183. value:=tordconstnode(n).value
  184. else
  185. IncompatibleTypes(n.resultdef, def);
  186. case def.floattype of
  187. s32real :
  188. list.concat(Tai_real_32bit.Create(ts32real(value)));
  189. s64real :
  190. {$ifdef ARM}
  191. if (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  192. not(cs_fp_emulation in current_settings.moduleswitches) then
  193. list.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  194. else
  195. {$endif ARM}
  196. list.concat(Tai_real_64bit.Create(ts64real(value)));
  197. s80real :
  198. list.concat(Tai_real_80bit.Create(value));
  199. s64comp :
  200. { the round is necessary for native compilers where comp isn't a float }
  201. list.concat(Tai_comp_64bit.Create(round(value)));
  202. s64currency:
  203. list.concat(Tai_comp_64bit.Create(round(value*10000)));
  204. s128real:
  205. list.concat(Tai_real_128bit.Create(value));
  206. else
  207. internalerror(200611053);
  208. end;
  209. n.free;
  210. end;
  211. procedure parse_classrefdef(list:tasmlist;def:tclassrefdef);
  212. var
  213. n : tnode;
  214. begin
  215. n:=comp_expr(true);
  216. case n.nodetype of
  217. loadvmtaddrn:
  218. begin
  219. if not Tobjectdef(tclassrefdef(n.resultdef).pointeddef).is_related(tobjectdef(def.pointeddef)) then
  220. IncompatibleTypes(n.resultdef, def);
  221. list.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(n.resultdef).pointeddef).vmt_mangledname)));
  222. end;
  223. niln:
  224. list.concat(Tai_const.Create_sym(nil));
  225. else
  226. IncompatibleTypes(n.resultdef, def);
  227. end;
  228. n.free;
  229. end;
  230. procedure parse_pointerdef(list:tasmlist;def:tpointerdef);
  231. var
  232. hp,p : tnode;
  233. srsym : tsym;
  234. pd : tprocdef;
  235. ca : pchar;
  236. pw : pcompilerwidestring;
  237. i,len : longint;
  238. base,
  239. offset : aint;
  240. ll : tasmlabel;
  241. varalign : shortint;
  242. begin
  243. p:=comp_expr(true);
  244. { remove equal typecasts for pointer/nil addresses }
  245. if (p.nodetype=typeconvn) then
  246. with Ttypeconvnode(p) do
  247. if (left.nodetype in [addrn,niln]) and equal_defs(def,p.resultdef) then
  248. begin
  249. hp:=left;
  250. left:=nil;
  251. p.free;
  252. p:=hp;
  253. end;
  254. { allows horrible ofs(typeof(TButton)^) code !! }
  255. if (p.nodetype=addrn) then
  256. with Taddrnode(p) do
  257. if left.nodetype=derefn then
  258. begin
  259. hp:=tderefnode(left).left;
  260. tderefnode(left).left:=nil;
  261. p.free;
  262. p:=hp;
  263. end;
  264. { const pointer ? }
  265. if (p.nodetype = pointerconstn) then
  266. begin
  267. if sizeof(TConstPtrUInt)=8 then
  268. list.concat(Tai_const.Create_64bit(int64(tpointerconstnode(p).value)))
  269. else
  270. if sizeof(TConstPtrUInt)=4 then
  271. list.concat(Tai_const.Create_32bit(longint(tpointerconstnode(p).value)))
  272. else
  273. internalerror(200404122);
  274. end
  275. { nil pointer ? }
  276. else if p.nodetype=niln then
  277. list.concat(Tai_const.Create_sym(nil))
  278. { maybe pchar ? }
  279. else
  280. if is_char(def.pointeddef) and
  281. (p.nodetype<>addrn) then
  282. begin
  283. current_asmdata.getdatalabel(ll);
  284. list.concat(Tai_const.Create_sym(ll));
  285. if p.nodetype=stringconstn then
  286. varalign:=size_2_align(tstringconstnode(p).len)
  287. else
  288. varalign:=0;
  289. varalign:=const_align(varalign);
  290. current_asmdata.asmlists[al_const].concat(Tai_align.Create(varalign));
  291. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  292. if p.nodetype=stringconstn then
  293. begin
  294. len:=tstringconstnode(p).len;
  295. { For tp7 the maximum lentgh can be 255 }
  296. if (m_tp7 in current_settings.modeswitches) and
  297. (len>255) then
  298. len:=255;
  299. getmem(ca,len+2);
  300. move(tstringconstnode(p).value_str^,ca^,len+1);
  301. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  302. end
  303. else
  304. if is_constcharnode(p) then
  305. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  306. else
  307. IncompatibleTypes(p.resultdef, def);
  308. end
  309. { maybe pwidechar ? }
  310. else
  311. if is_widechar(def.pointeddef) and
  312. (p.nodetype<>addrn) then
  313. begin
  314. current_asmdata.getdatalabel(ll);
  315. list.concat(Tai_const.Create_sym(ll));
  316. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(aint))));
  317. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  318. if (p.nodetype in [stringconstn,ordconstn]) then
  319. begin
  320. { convert to widestring stringconstn }
  321. inserttypeconv(p,cwidestringtype);
  322. if (p.nodetype=stringconstn) and
  323. (tstringconstnode(p).cst_type=cst_widestring) then
  324. begin
  325. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  326. for i:=0 to tstringconstnode(p).len-1 do
  327. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  328. { ending #0 }
  329. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  330. end;
  331. end
  332. else
  333. IncompatibleTypes(p.resultdef, def);
  334. end
  335. else
  336. if (p.nodetype=addrn) or
  337. is_procvar_load(p) then
  338. begin
  339. { insert typeconv }
  340. inserttypeconv(p,def);
  341. hp:=p;
  342. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  343. hp:=tunarynode(hp).left;
  344. if (hp.nodetype=loadn) then
  345. begin
  346. hp:=p;
  347. offset:=0;
  348. while assigned(hp) and (hp.nodetype<>loadn) do
  349. begin
  350. case hp.nodetype of
  351. vecn :
  352. begin
  353. case tvecnode(hp).left.resultdef.typ of
  354. stringdef :
  355. begin
  356. { this seems OK for shortstring and ansistrings PM }
  357. { it is wrong for widestrings !! }
  358. len:=1;
  359. base:=0;
  360. end;
  361. arraydef :
  362. begin
  363. if not is_packed_array(tvecnode(hp).left.resultdef) then
  364. begin
  365. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  366. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  367. end
  368. else
  369. begin
  370. Message(parser_e_packed_dynamic_open_array);
  371. len:=1;
  372. base:=0;
  373. end;
  374. end
  375. else
  376. Message(parser_e_illegal_expression);
  377. end;
  378. if is_constintnode(tvecnode(hp).right) then
  379. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  380. else
  381. Message(parser_e_illegal_expression);
  382. end;
  383. subscriptn :
  384. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  385. typeconvn :
  386. begin
  387. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  388. Message(parser_e_illegal_expression);
  389. end;
  390. addrn :
  391. ;
  392. else
  393. Message(parser_e_illegal_expression);
  394. end;
  395. hp:=tunarynode(hp).left;
  396. end;
  397. srsym:=tloadnode(hp).symtableentry;
  398. case srsym.typ of
  399. procsym :
  400. begin
  401. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  402. if Tprocsym(srsym).ProcdefList.Count>1 then
  403. Message(parser_e_no_overloaded_procvars);
  404. if po_abstractmethod in pd.procoptions then
  405. Message(type_e_cant_take_address_of_abstract_method)
  406. else
  407. list.concat(Tai_const.Createname(pd.mangledname,offset));
  408. end;
  409. staticvarsym :
  410. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  411. labelsym :
  412. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  413. constsym :
  414. if tconstsym(srsym).consttyp=constresourcestring then
  415. list.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',tconstsym(srsym).owner,''),tconstsym(srsym).resstrindex*(4+sizeof(aint)*3)+4+sizeof(aint)))
  416. else
  417. Message(type_e_variable_id_expected);
  418. else
  419. Message(type_e_variable_id_expected);
  420. end;
  421. end
  422. else
  423. Message(parser_e_illegal_expression);
  424. end
  425. else
  426. { allow typeof(Object type)}
  427. if (p.nodetype=inlinen) and
  428. (tinlinenode(p).inlinenumber=in_typeof_x) then
  429. begin
  430. if (tinlinenode(p).left.nodetype=typen) then
  431. begin
  432. list.concat(Tai_const.createname(
  433. tobjectdef(tinlinenode(p).left.resultdef).vmt_mangledname,0));
  434. end
  435. else
  436. Message(parser_e_illegal_expression);
  437. end
  438. else
  439. Message(parser_e_illegal_expression);
  440. p.free;
  441. end;
  442. procedure parse_setdef(list:tasmlist;def:tsetdef);
  443. type
  444. setbytes = array[0..31] of byte;
  445. Psetbytes = ^setbytes;
  446. var
  447. p : tnode;
  448. i,j : longint;
  449. begin
  450. p:=comp_expr(true);
  451. if p.nodetype=setconstn then
  452. begin
  453. { be sure to convert to the correct result, else
  454. it can generate smallset data instead of normalset (PFV) }
  455. inserttypeconv(p,def);
  456. { we only allow const sets }
  457. if assigned(tsetconstnode(p).left) then
  458. Message(parser_e_illegal_expression)
  459. else
  460. begin
  461. { this writing is endian independant }
  462. { untrue - because they are considered }
  463. { arrays of 32-bit values CEC }
  464. if source_info.endian = target_info.endian then
  465. begin
  466. for i:=0 to p.resultdef.size-1 do
  467. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[i]));
  468. end
  469. else
  470. begin
  471. { store as longint values in swaped format }
  472. j:=0;
  473. for i:=0 to ((p.resultdef.size-1) div 4) do
  474. begin
  475. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  476. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  477. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  478. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  479. Inc(j,4);
  480. end;
  481. end;
  482. end;
  483. end
  484. else
  485. Message(parser_e_illegal_expression);
  486. p.free;
  487. end;
  488. procedure parse_enumdef(list:tasmlist;def:tenumdef);
  489. var
  490. p : tnode;
  491. begin
  492. p:=comp_expr(true);
  493. if p.nodetype=ordconstn then
  494. begin
  495. if equal_defs(p.resultdef,def) or
  496. is_subequal(p.resultdef,def) then
  497. begin
  498. case longint(p.resultdef.size) of
  499. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  500. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  501. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  502. end;
  503. end
  504. else
  505. IncompatibleTypes(p.resultdef,def);
  506. end
  507. else
  508. Message(parser_e_illegal_expression);
  509. p.free;
  510. end;
  511. procedure parse_stringdef(list:tasmlist;def:tstringdef);
  512. var
  513. n : tnode;
  514. i : longint;
  515. strlength : aint;
  516. strval : pchar;
  517. strch : char;
  518. ll : tasmlabel;
  519. ca : pchar;
  520. begin
  521. n:=comp_expr(true);
  522. { load strval and strlength of the constant tree }
  523. if (n.nodetype=stringconstn) or is_widestring(def) then
  524. begin
  525. { convert to the expected string type so that
  526. for widestrings strval is a pcompilerwidestring }
  527. inserttypeconv(n,def);
  528. strlength:=tstringconstnode(n).len;
  529. strval:=tstringconstnode(n).value_str;
  530. end
  531. else if is_constcharnode(n) then
  532. begin
  533. { strval:=pchar(@tordconstnode(n).value);
  534. THIS FAIL on BIG_ENDIAN MACHINES PM }
  535. strch:=chr(tordconstnode(n).value and $ff);
  536. strval:=@strch;
  537. strlength:=1
  538. end
  539. else if is_constresourcestringnode(n) then
  540. begin
  541. strval:=pchar(tconstsym(tloadnode(n).symtableentry).value.valueptr);
  542. strlength:=tconstsym(tloadnode(n).symtableentry).value.len;
  543. end
  544. else
  545. begin
  546. Message(parser_e_illegal_expression);
  547. strlength:=-1;
  548. end;
  549. if strlength>=0 then
  550. begin
  551. case def.stringtype of
  552. st_shortstring:
  553. begin
  554. if strlength>=def.size then
  555. begin
  556. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  557. strlength:=def.size-1;
  558. end;
  559. list.concat(Tai_const.Create_8bit(strlength));
  560. { this can also handle longer strings }
  561. getmem(ca,strlength+1);
  562. move(strval^,ca^,strlength);
  563. ca[strlength]:=#0;
  564. list.concat(Tai_string.Create_pchar(ca,strlength));
  565. { fillup with spaces if size is shorter }
  566. if def.size>strlength then
  567. begin
  568. getmem(ca,def.size-strlength);
  569. { def.size contains also the leading length, so we }
  570. { we have to subtract one }
  571. fillchar(ca[0],def.size-strlength-1,' ');
  572. ca[def.size-strlength-1]:=#0;
  573. { this can also handle longer strings }
  574. list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  575. end;
  576. end;
  577. st_ansistring:
  578. begin
  579. { an empty ansi string is nil! }
  580. if (strlength=0) then
  581. list.concat(Tai_const.Create_sym(nil))
  582. else
  583. begin
  584. current_asmdata.getdatalabel(ll);
  585. list.concat(Tai_const.Create_sym(ll));
  586. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  587. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  588. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength));
  589. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  590. getmem(ca,strlength+1);
  591. move(strval^,ca^,strlength);
  592. { The terminating #0 to be stored in the .data section (JM) }
  593. ca[strlength]:=#0;
  594. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,strlength+1));
  595. end;
  596. end;
  597. st_widestring:
  598. begin
  599. { an empty ansi string is nil! }
  600. if (strlength=0) then
  601. list.concat(Tai_const.Create_sym(nil))
  602. else
  603. begin
  604. current_asmdata.getdatalabel(ll);
  605. list.concat(Tai_const.Create_sym(ll));
  606. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  607. if tf_winlikewidestring in target_info.flags then
  608. current_asmdata.asmlists[al_const].concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  609. else
  610. begin
  611. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  612. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength*cwidechartype.size));
  613. end;
  614. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  615. for i:=0 to strlength-1 do
  616. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  617. { ending #0 }
  618. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(0))
  619. end;
  620. end;
  621. else
  622. internalerror(200107081);
  623. end;
  624. end;
  625. n.free;
  626. end;
  627. procedure flush_packed_value(list: tasmlist; var curval, nextval: aword; loadbitsize: byte; var curbitoffset: smallint);
  628. begin
  629. { these values have to be byte swapped when cross-compiling }
  630. { from one endianess to another, but this will be done }
  631. { automatically by the assembler writer }
  632. case loadbitsize of
  633. 8: list.concat(tai_const.create_8bit(curval));
  634. 16: list.concat(tai_const.create_16bit(curval));
  635. 32: list.concat(tai_const.create_32bit(curval));
  636. 64: list.concat(tai_const.create_64bit(curval));
  637. else
  638. internalerror(2007022011);
  639. end;
  640. curval:=nextval;
  641. nextval:=0;
  642. dec(curbitoffset,loadbitsize);
  643. end;
  644. { parse a single constant and add it to the packed const info }
  645. { represented by curval etc (see explanation of bitpackval for }
  646. { what the different parameters mean) }
  647. function parse_single_packed_const(list: tasmlist; def: tdef; var curval: aword; out nextval: aword; loadbitsize, packedbitsize: byte; var curbitoffset: smallint): boolean;
  648. var
  649. n : tnode;
  650. begin
  651. result:=true;
  652. n:=comp_expr(true);
  653. if (n.nodetype <> ordconstn) or
  654. not equal_defs(n.resultdef,def) and
  655. not is_subequal(n.resultdef,def) then
  656. begin
  657. n.free;
  658. incompatibletypes(n.resultdef,def);
  659. consume_all_until(_SEMICOLON);
  660. result:=false;
  661. exit;
  662. end;
  663. bitpackval(tordconstnode(n).value,curval,nextval,loadbitsize,packedbitsize,curbitoffset);
  664. if (curbitoffset>=loadbitsize) then
  665. flush_packed_value(list,curval,nextval,loadbitsize,curbitoffset);
  666. n.free;
  667. end;
  668. { parses a packed array constant }
  669. procedure parse_packed_array_def(list: tasmlist; def: tarraydef);
  670. var
  671. i : aint;
  672. curval, nextval: aword;
  673. curbitoffset: smallint;
  674. packedbitsize,
  675. loadbitsize: byte;
  676. begin
  677. if not(def.elementdef.typ in [orddef,enumdef]) then
  678. internalerror(2007022010);
  679. { begin of the array }
  680. consume(_LKLAMMER);
  681. packedbitsize:=def.elepackedbitsize;
  682. loadbitsize:=packedbitsloadsize(packedbitsize)*8;
  683. curval:=0;
  684. curbitoffset:=0;
  685. i:=def.lowrange;
  686. { can't use for-loop, fails when cross-compiling from }
  687. { 32 to 64 bit because i is then 64 bit }
  688. while (i<def.highrange) do
  689. begin
  690. { get next item of the packed array }
  691. if not parse_single_packed_const(list,def.elementdef,curval,nextval,loadbitsize,packedbitsize,curbitoffset) then
  692. exit;
  693. consume(_COMMA);
  694. inc(i);
  695. end;
  696. { final item }
  697. if not parse_single_packed_const(list,def.elementdef,curval,nextval,loadbitsize,packedbitsize,curbitoffset) then
  698. exit;
  699. { flush final incomplete value if necessary }
  700. if (curbitoffset <> 0) then
  701. flush_packed_value(list,curval,nextval,loadbitsize,curbitoffset);
  702. consume(_RKLAMMER);
  703. end;
  704. procedure parse_arraydef(list:tasmlist;def:tarraydef);
  705. var
  706. n : tnode;
  707. i : longint;
  708. len : aint;
  709. ch : char;
  710. ca : pchar;
  711. begin
  712. { dynamic array nil }
  713. if is_dynamic_array(def) then
  714. begin
  715. { Only allow nil initialization }
  716. consume(_NIL);
  717. list.concat(Tai_const.Create_sym(nil));
  718. end
  719. { packed array constant }
  720. else if is_packed_array(def) and
  721. (def.elepackedbitsize mod 8 <> 0) then
  722. begin
  723. parse_packed_array_def(list,def);
  724. end
  725. { normal array const between brackets }
  726. else if try_to_consume(_LKLAMMER) then
  727. begin
  728. for i:=def.lowrange to def.highrange-1 do
  729. begin
  730. read_typed_const_data(list,def.elementdef);
  731. consume(_COMMA);
  732. end;
  733. read_typed_const_data(list,def.elementdef);
  734. consume(_RKLAMMER);
  735. end
  736. { if array of char then we allow also a string }
  737. else if is_char(def.elementdef) then
  738. begin
  739. n:=comp_expr(true);
  740. if n.nodetype=stringconstn then
  741. begin
  742. len:=tstringconstnode(n).len;
  743. { For tp7 the maximum lentgh can be 255 }
  744. if (m_tp7 in current_settings.modeswitches) and
  745. (len>255) then
  746. len:=255;
  747. ca:=tstringconstnode(n).value_str;
  748. end
  749. else
  750. if is_constcharnode(n) then
  751. begin
  752. ch:=chr(tordconstnode(n).value and $ff);
  753. ca:=@ch;
  754. len:=1;
  755. end
  756. else
  757. begin
  758. Message(parser_e_illegal_expression);
  759. len:=0;
  760. end;
  761. if len>(def.highrange-def.lowrange+1) then
  762. Message(parser_e_string_larger_array);
  763. for i:=def.lowrange to def.highrange do
  764. begin
  765. if i+1-def.lowrange<=len then
  766. begin
  767. list.concat(Tai_const.Create_8bit(byte(ca^)));
  768. inc(ca);
  769. end
  770. else
  771. {Fill the remaining positions with #0.}
  772. list.concat(Tai_const.Create_8bit(0));
  773. end;
  774. n.free;
  775. end
  776. else
  777. begin
  778. { we want the ( }
  779. consume(_LKLAMMER);
  780. end;
  781. end;
  782. procedure parse_procvardef(list:tasmlist;def:tprocvardef);
  783. var
  784. tmpn,n : tnode;
  785. pd : tprocdef;
  786. begin
  787. { Procvars and pointers are no longer compatible. }
  788. { under tp: =nil or =var under fpc: =nil or =@var }
  789. if try_to_consume(_NIL) then
  790. begin
  791. list.concat(Tai_const.Create_sym(nil));
  792. if (po_methodpointer in def.procoptions) then
  793. list.concat(Tai_const.Create_sym(nil));
  794. exit;
  795. end;
  796. { you can't assign a value other than NIL to a typed constant }
  797. { which is a "procedure of object", because this also requires }
  798. { address of an object/class instance, which is not known at }
  799. { compile time (JM) }
  800. if (po_methodpointer in def.procoptions) then
  801. Message(parser_e_no_procvarobj_const);
  802. { parse the rest too, so we can continue with error checking }
  803. getprocvardef:=def;
  804. n:=comp_expr(true);
  805. getprocvardef:=nil;
  806. if codegenerror then
  807. begin
  808. n.free;
  809. exit;
  810. end;
  811. { let type conversion check everything needed }
  812. inserttypeconv(n,def);
  813. if codegenerror then
  814. begin
  815. n.free;
  816. exit;
  817. end;
  818. { remove typeconvs, that will normally insert a lea
  819. instruction which is not necessary for us }
  820. while n.nodetype=typeconvn do
  821. begin
  822. tmpn:=ttypeconvnode(n).left;
  823. ttypeconvnode(n).left:=nil;
  824. n.free;
  825. n:=tmpn;
  826. end;
  827. { remove addrn which we also don't need here }
  828. if n.nodetype=addrn then
  829. begin
  830. tmpn:=taddrnode(n).left;
  831. taddrnode(n).left:=nil;
  832. n.free;
  833. n:=tmpn;
  834. end;
  835. { we now need to have a loadn with a procsym }
  836. if (n.nodetype=loadn) and
  837. (tloadnode(n).symtableentry.typ=procsym) then
  838. begin
  839. pd:=tprocdef(tprocsym(tloadnode(n).symtableentry).ProcdefList[0]);
  840. list.concat(Tai_const.createname(pd.mangledname,0));
  841. end
  842. else
  843. Message(parser_e_illegal_expression);
  844. n.free;
  845. end;
  846. procedure parse_recorddef(list:tasmlist;def:trecorddef);
  847. var
  848. n : tnode;
  849. i,
  850. symidx : longint;
  851. recsym,
  852. srsym : tsym;
  853. hs : string;
  854. sorg,s : TIDString;
  855. tmpguid : tguid;
  856. curroffset : aint;
  857. error : boolean;
  858. begin
  859. { no packed record support }
  860. if is_packed_record_or_object(def) then
  861. begin
  862. Message(type_e_no_const_packed_record);
  863. exit;
  864. end;
  865. { GUID }
  866. if (def=rec_tguid) and
  867. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  868. begin
  869. n:=comp_expr(true);
  870. inserttypeconv(n,cshortstringtype);
  871. if n.nodetype=stringconstn then
  872. begin
  873. hs:=strpas(tstringconstnode(n).value_str);
  874. if string2guid(hs,tmpguid) then
  875. begin
  876. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  877. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  878. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  879. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  880. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  881. end
  882. else
  883. Message(parser_e_improper_guid_syntax);
  884. end
  885. else
  886. Message(parser_e_illegal_expression);
  887. n.free;
  888. exit;
  889. end;
  890. { normal record }
  891. consume(_LKLAMMER);
  892. curroffset:=0;
  893. symidx:=0;
  894. sorg:='';
  895. srsym:=tsym(def.symtable.SymList[symidx]);
  896. recsym := nil;
  897. while token<>_RKLAMMER do
  898. begin
  899. s:=pattern;
  900. sorg:=orgpattern;
  901. consume(_ID);
  902. consume(_COLON);
  903. error := false;
  904. recsym := tsym(def.symtable.Find(s));
  905. if not assigned(recsym) then
  906. begin
  907. Message1(sym_e_illegal_field,sorg);
  908. error := true;
  909. end;
  910. if (not error) and
  911. (not assigned(srsym) or
  912. (s <> srsym.name)) then
  913. { possible variant record (JM) }
  914. begin
  915. { All parts of a variant start at the same offset }
  916. { Also allow jumping from one variant part to another, }
  917. { as long as the offsets match }
  918. if (assigned(srsym) and
  919. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  920. { srsym is not assigned after parsing w2 in the }
  921. { typed const in the next example: }
  922. { type tr = record case byte of }
  923. { 1: (l1,l2: dword); }
  924. { 2: (w1,w2: word); }
  925. { end; }
  926. { const r: tr = (w1:1;w2:1;l2:5); }
  927. (tfieldvarsym(recsym).fieldoffset = curroffset) then
  928. srsym := recsym
  929. { going backwards isn't allowed in any mode }
  930. else if (tfieldvarsym(recsym).fieldoffset<curroffset) then
  931. begin
  932. Message(parser_e_invalid_record_const);
  933. error := true;
  934. end
  935. { Delphi allows you to skip fields }
  936. else if (m_delphi in current_settings.modeswitches) then
  937. begin
  938. Message1(parser_w_skipped_fields_before,sorg);
  939. srsym := recsym;
  940. end
  941. { FPC and TP don't }
  942. else
  943. begin
  944. Message1(parser_e_skipped_fields_before,sorg);
  945. error := true;
  946. end;
  947. end;
  948. if error then
  949. consume_all_until(_SEMICOLON)
  950. else
  951. begin
  952. { if needed fill (alignment) }
  953. if tfieldvarsym(srsym).fieldoffset>curroffset then
  954. for i:=1 to tfieldvarsym(srsym).fieldoffset-curroffset do
  955. list.concat(Tai_const.Create_8bit(0));
  956. { new position }
  957. curroffset:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vardef.size;
  958. { read the data }
  959. read_typed_const_data(list,tfieldvarsym(srsym).vardef);
  960. { keep previous field for checking whether whole }
  961. { record was initialized (JM) }
  962. recsym := srsym;
  963. { goto next field }
  964. inc(symidx);
  965. if symidx<def.symtable.SymList.Count then
  966. srsym:=tsym(def.symtable.SymList[symidx])
  967. else
  968. srsym:=nil;
  969. if token=_SEMICOLON then
  970. consume(_SEMICOLON)
  971. else break;
  972. end;
  973. end;
  974. { are there any fields left, but don't complain if there only
  975. come other variant partsa fter the last initialized field }
  976. if assigned(srsym) and
  977. (
  978. (recsym=nil) or
  979. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  980. ) then
  981. Message1(parser_w_skipped_fields_after,sorg);
  982. for i:=1 to def.size-curroffset do
  983. list.concat(Tai_const.Create_8bit(0));
  984. consume(_RKLAMMER);
  985. end;
  986. procedure parse_objectdef(list:tasmlist;def:tobjectdef);
  987. var
  988. n : tnode;
  989. i : longint;
  990. obj : tobjectdef;
  991. srsym : tsym;
  992. st : tsymtable;
  993. curroffset : aint;
  994. s,sorg : TIDString;
  995. vmtwritten : boolean;
  996. begin
  997. { no support for packed object }
  998. if is_packed_record_or_object(def) then
  999. begin
  1000. Message(type_e_no_const_packed_record);
  1001. exit;
  1002. end;
  1003. { only allow nil for class and interface }
  1004. if is_class_or_interface(def) then
  1005. begin
  1006. n:=comp_expr(true);
  1007. if n.nodetype<>niln then
  1008. begin
  1009. Message(parser_e_type_const_not_possible);
  1010. consume_all_until(_SEMICOLON);
  1011. end
  1012. else
  1013. list.concat(Tai_const.Create_sym(nil));
  1014. n.free;
  1015. exit;
  1016. end;
  1017. { for objects we allow it only if it doesn't contain a vmt }
  1018. if (oo_has_vmt in def.objectoptions) and
  1019. (m_fpc in current_settings.modeswitches) then
  1020. begin
  1021. Message(parser_e_type_object_constants);
  1022. exit;
  1023. end;
  1024. consume(_LKLAMMER);
  1025. curroffset:=0;
  1026. vmtwritten:=false;
  1027. while token<>_RKLAMMER do
  1028. begin
  1029. s:=pattern;
  1030. sorg:=orgpattern;
  1031. consume(_ID);
  1032. consume(_COLON);
  1033. srsym:=nil;
  1034. obj:=tobjectdef(def);
  1035. st:=obj.symtable;
  1036. while (srsym=nil) and assigned(st) do
  1037. begin
  1038. srsym:=tsym(st.Find(s));
  1039. if assigned(obj) then
  1040. obj:=obj.childof;
  1041. if assigned(obj) then
  1042. st:=obj.symtable
  1043. else
  1044. st:=nil;
  1045. end;
  1046. if srsym=nil then
  1047. begin
  1048. Message1(sym_e_id_not_found,sorg);
  1049. consume_all_until(_SEMICOLON);
  1050. end
  1051. else
  1052. with tfieldvarsym(srsym) do
  1053. begin
  1054. { check position }
  1055. if fieldoffset<curroffset then
  1056. message(parser_e_invalid_record_const);
  1057. { check in VMT needs to be added for TP mode }
  1058. if not(vmtwritten) and
  1059. not(m_fpc in current_settings.modeswitches) and
  1060. (oo_has_vmt in def.objectoptions) and
  1061. (def.vmt_offset<fieldoffset) then
  1062. begin
  1063. for i:=1 to def.vmt_offset-curroffset do
  1064. list.concat(tai_const.create_8bit(0));
  1065. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1066. { this is more general }
  1067. curroffset:=def.vmt_offset + sizeof(aint);
  1068. vmtwritten:=true;
  1069. end;
  1070. { if needed fill }
  1071. if fieldoffset>curroffset then
  1072. for i:=1 to fieldoffset-curroffset do
  1073. list.concat(Tai_const.Create_8bit(0));
  1074. { new position }
  1075. curroffset:=fieldoffset+vardef.size;
  1076. { read the data }
  1077. read_typed_const_data(list,vardef);
  1078. if not try_to_consume(_SEMICOLON) then
  1079. break;
  1080. end;
  1081. end;
  1082. if not(m_fpc in current_settings.modeswitches) and
  1083. (oo_has_vmt in def.objectoptions) and
  1084. (def.vmt_offset>=curroffset) then
  1085. begin
  1086. for i:=1 to def.vmt_offset-curroffset do
  1087. list.concat(tai_const.create_8bit(0));
  1088. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1089. { this is more general }
  1090. curroffset:=def.vmt_offset + sizeof(aint);
  1091. end;
  1092. for i:=1 to def.size-curroffset do
  1093. list.concat(Tai_const.Create_8bit(0));
  1094. consume(_RKLAMMER);
  1095. end;
  1096. var
  1097. old_block_type : tblock_type;
  1098. begin
  1099. old_block_type:=block_type;
  1100. block_type:=bt_const;
  1101. case def.typ of
  1102. orddef :
  1103. parse_orddef(list,torddef(def));
  1104. floatdef :
  1105. parse_floatdef(list,tfloatdef(def));
  1106. classrefdef :
  1107. parse_classrefdef(list,tclassrefdef(def));
  1108. pointerdef :
  1109. parse_pointerdef(list,tpointerdef(def));
  1110. setdef :
  1111. parse_setdef(list,tsetdef(def));
  1112. enumdef :
  1113. parse_enumdef(list,tenumdef(def));
  1114. stringdef :
  1115. parse_stringdef(list,tstringdef(def));
  1116. arraydef :
  1117. parse_arraydef(list,tarraydef(def));
  1118. procvardef:
  1119. parse_procvardef(list,tprocvardef(def));
  1120. recorddef:
  1121. parse_recorddef(list,trecorddef(def));
  1122. objectdef:
  1123. parse_objectdef(list,tobjectdef(def));
  1124. errordef:
  1125. begin
  1126. { try to consume something useful }
  1127. if token=_LKLAMMER then
  1128. consume_all_until(_RKLAMMER)
  1129. else
  1130. consume_all_until(_SEMICOLON);
  1131. end;
  1132. else
  1133. Message(parser_e_type_const_not_possible);
  1134. end;
  1135. block_type:=old_block_type;
  1136. end;
  1137. {$maxfpuregisters default}
  1138. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  1139. var
  1140. storefilepos : tfileposinfo;
  1141. cursectype : TAsmSectionType;
  1142. C_name : string;
  1143. begin
  1144. { mark the staticvarsym as typedconst }
  1145. include(sym.varoptions,vo_is_typed_const);
  1146. { The variable has a value assigned }
  1147. sym.varstate:=vs_initialised;
  1148. { the variable can't be placed in a register }
  1149. sym.varregable:=vr_none;
  1150. { generate data for typed const }
  1151. storefilepos:=current_filepos;
  1152. current_filepos:=sym.fileinfo;
  1153. if sym.varspez=vs_const then
  1154. cursectype:=sec_rodata
  1155. else
  1156. cursectype:=sec_data;
  1157. maybe_new_object_file(list);
  1158. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  1159. if (sym.owner.symtabletype=globalsymtable) or
  1160. maybe_smartlink_symbol or
  1161. (assigned(current_procinfo) and
  1162. (po_inline in current_procinfo.procdef.procoptions)) or
  1163. DLLSource then
  1164. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1165. else
  1166. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1167. read_typed_const_data(list,sym.vardef);
  1168. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1169. current_filepos:=storefilepos;
  1170. { Parse hints }
  1171. try_consume_hintdirective(sym.symoptions);
  1172. { Support public name directive }
  1173. if try_to_consume(_PUBLIC) then
  1174. begin
  1175. include(sym.varoptions,vo_is_public);
  1176. if try_to_consume(_NAME) then
  1177. C_name:=get_stringconst
  1178. else
  1179. C_name:=sym.realname;
  1180. sym.set_mangledname(C_Name);
  1181. end;
  1182. end;
  1183. end.