ptconst.pas 45 KB

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