ptconst.pas 43 KB

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