ptconst.pas 45 KB

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