ptconst.pas 50 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;
  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
  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. curconstsegment : TAAsmoutput;
  58. ll : tasmlabel;
  59. s,sorg : string;
  60. c : char;
  61. ca : pchar;
  62. tmpguid : tguid;
  63. aktpos : longint;
  64. obj : tobjectdef;
  65. recsym,
  66. srsym : tsym;
  67. symt : tsymtable;
  68. value : bestreal;
  69. intvalue : tconstexprint;
  70. strval : pchar;
  71. pw : pcompilerwidestring;
  72. error : boolean;
  73. old_block_type : tblock_type;
  74. storefilepos : tfileposinfo;
  75. sizelabel : tasmlabel;
  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. { put everything in the datasemgent to prevent
  91. mixing array indexes with ansistring data }
  92. curconstsegment:=datasegment;
  93. if assigned(sym) then
  94. begin
  95. storefilepos:=aktfilepos;
  96. aktfilepos:=sym.fileinfo;
  97. l:=sym.getsize;
  98. { insert cut for smartlinking or alignment }
  99. maybe_new_object_file(curconstSegment);
  100. new_section(curconstSegment,sec_rodata,lower(sym.mangledname),const_align(l));
  101. {$ifdef GDB}
  102. if (cs_debuginfo in aktmoduleswitches) then
  103. sym.concatstabto(curconstSegment);
  104. {$endif GDB}
  105. if (sym.owner.symtabletype=globalsymtable) or
  106. maybe_smartlink_symbol or
  107. (assigned(current_procinfo) and
  108. (po_inline in current_procinfo.procdef.procoptions)) or
  109. DLLSource then
  110. curconstSegment.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,l))
  111. else
  112. curconstSegment.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,l));
  113. aktfilepos:=storefilepos;
  114. end;
  115. case t.def.deftype of
  116. orddef:
  117. begin
  118. p:=comp_expr(true);
  119. case torddef(t.def).typ of
  120. bool8bit :
  121. begin
  122. if is_constboolnode(p) then
  123. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  124. else
  125. Message(parser_e_illegal_expression);
  126. end;
  127. bool16bit :
  128. begin
  129. if is_constboolnode(p) then
  130. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  131. else
  132. Message(parser_e_illegal_expression);
  133. end;
  134. bool32bit :
  135. begin
  136. if is_constboolnode(p) then
  137. curconstSegment.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)))
  138. else
  139. Message(parser_e_illegal_expression);
  140. end;
  141. bool64bit :
  142. begin
  143. if is_constboolnode(p) then
  144. curconstSegment.concat(Tai_const.Create_64bit(int64(tordconstnode(p).value)))
  145. else
  146. Message(parser_e_illegal_expression);
  147. end;
  148. uchar :
  149. begin
  150. if is_constcharnode(p) then
  151. curconstSegment.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. curconstSegment.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. curconstSegment.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. curconstSegment.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. curconstSegment.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. curconstSegment.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. curconstSegment.concat(Tai_real_32bit.Create(ts32real(value)));
  235. s64real :
  236. {$ifdef ARM}
  237. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  238. curconstSegment.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  239. else
  240. {$endif ARM}
  241. curconstSegment.concat(Tai_real_64bit.Create(ts64real(value)));
  242. s80real :
  243. curconstSegment.concat(Tai_real_80bit.Create(value));
  244. { the round is necessary for native compilers where comp isn't a float }
  245. s64comp :
  246. curconstSegment.concat(Tai_comp_64bit.Create(round(value)));
  247. s64currency:
  248. curconstSegment.concat(Tai_comp_64bit.Create(round(value*10000)));
  249. s128real:
  250. curconstSegment.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. curconstSegment.concat(Tai_const.Create_sym(objectlibrary.newasmsymbol(
  266. Tobjectdef(pointertype.def).vmt_mangledname,AB_EXTERNAL,AT_DATA)));
  267. end;
  268. niln:
  269. curconstSegment.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. curconstsegment.concat(Tai_const.Create_64bit(TConstPtrUInt(tpointerconstnode(p).value)))
  301. else
  302. if sizeof(TConstPtrUInt)=4 then
  303. curconstsegment.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. curconstSegment.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. curconstSegment.concat(Tai_const.Create_sym(ll));
  317. if p.nodetype=stringconstn then
  318. varalign:=tstringconstnode(p).len
  319. else
  320. varalign:=0;
  321. varalign:=const_align(varalign);
  322. Consts.concat(Tai_align.Create(varalign));
  323. Consts.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. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  334. end
  335. else
  336. if is_constcharnode(p) then
  337. Consts.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. curconstSegment.concat(Tai_const.Create_sym(ll));
  348. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  349. Consts.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. Consts.concat(Tai_const.Create_16bit(pw^.data[i]));
  360. { ending #0 }
  361. Consts.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. curconstSegment.concat(Tai_const.Createname(tprocsym(srsym).first_procdef.mangledname,AT_FUNCTION,offset));
  430. end;
  431. globalvarsym :
  432. curconstSegment.concat(Tai_const.Createname(tglobalvarsym(srsym).mangledname,AT_DATA,offset));
  433. typedconstsym :
  434. curconstSegment.concat(Tai_const.Createname(ttypedconstsym(srsym).mangledname,AT_DATA,offset));
  435. labelsym :
  436. curconstSegment.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,AT_FUNCTION,offset));
  437. constsym :
  438. if tconstsym(srsym).consttyp=constresourcestring then
  439. curconstSegment.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. curconstSegment.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. curconstsegment.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. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  494. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  495. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  496. curconstsegment.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 : curconstSegment.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  516. 2 : curconstSegment.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  517. 4 : curconstSegment.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. curconstSegment.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. curconstSegment.concat(Tai_string.Create_length_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. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  583. end;
  584. end;
  585. {$ifdef ansistrings_bits}
  586. st_ansistring16:
  587. begin
  588. { an empty ansi string is nil! }
  589. if (strlength=0) then
  590. curconstSegment.concat(Tai_const.Create_ptr(0))
  591. else
  592. begin
  593. objectlibrary.getdatalabel(ll);
  594. curconstSegment.concat(Tai_const_symbol.Create(ll));
  595. { the actual structure starts at -12 from start label - CEC }
  596. Consts.concat(tai_align.create(const_align(pointer_size)));
  597. { first write the maximum size }
  598. Consts.concat(Tai_const.Create_16bit(strlength));
  599. { second write the real length }
  600. Consts.concat(Tai_const.Create_16bit(strlength));
  601. { redondent with maxlength but who knows ... (PM) }
  602. { third write use count (set to -1 for safety ) }
  603. Consts.concat(Tai_const.Create_16bit(-1));
  604. Consts.concat(Tai_label.Create(ll));
  605. getmem(ca,strlength+2);
  606. move(strval^,ca^,strlength);
  607. { The terminating #0 to be stored in the .data section (JM) }
  608. ca[strlength]:=#0;
  609. { End of the PChar. The memory has to be allocated because in }
  610. { tai_string.done, there is a freemem(len+1) (JM) }
  611. ca[strlength+1]:=#0;
  612. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  613. end;
  614. end;
  615. {$endif}
  616. {$ifdef ansistring_bits}st_ansistring32{$else}st_ansistring{$endif}:
  617. begin
  618. { an empty ansi string is nil! }
  619. if (strlength=0) then
  620. curconstSegment.concat(Tai_const.Create_sym(nil))
  621. else
  622. begin
  623. objectlibrary.getdatalabel(ll);
  624. curconstSegment.concat(Tai_const.Create_sym(ll));
  625. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  626. Consts.concat(Tai_const.Create_aint(-1));
  627. Consts.concat(Tai_const.Create_aint(strlength));
  628. Consts.concat(Tai_label.Create(ll));
  629. getmem(ca,strlength+2);
  630. move(strval^,ca^,strlength);
  631. { The terminating #0 to be stored in the .data section (JM) }
  632. ca[strlength]:=#0;
  633. { End of the PChar. The memory has to be allocated because in }
  634. { tai_string.done, there is a freemem(len+1) (JM) }
  635. ca[strlength+1]:=#0;
  636. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  637. end;
  638. end;
  639. {$ifdef ansistring_bits}
  640. st_ansistring64:
  641. begin
  642. { an empty ansi string is nil! }
  643. if (strlength=0) then
  644. curconstSegment.concat(Tai_const.Create_ptr(0))
  645. else
  646. begin
  647. objectlibrary.getdatalabel(ll);
  648. curconstSegment.concat(Tai_const_symbol.Create(ll));
  649. { the actual structure starts at -12 from start label - CEC }
  650. Consts.concat(tai_align.create(const_align(pointer_size)));
  651. { first write the maximum size }
  652. Consts.concat(Tai_const.Create_64bit(strlength));
  653. { second write the real length }
  654. Consts.concat(Tai_const.Create_64bit(strlength));
  655. { redondent with maxlength but who knows ... (PM) }
  656. { third write use count (set to -1 for safety ) }
  657. Consts.concat(Tai_const.Create_64bit(-1));
  658. Consts.concat(Tai_label.Create(ll));
  659. getmem(ca,strlength+2);
  660. move(strval^,ca^,strlength);
  661. { The terminating #0 to be stored in the .data section (JM) }
  662. ca[strlength]:=#0;
  663. { End of the PChar. The memory has to be allocated because in }
  664. { tai_string.done, there is a freemem(len+1) (JM) }
  665. ca[strlength+1]:=#0;
  666. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  667. end;
  668. end;
  669. {$endif}
  670. st_widestring:
  671. begin
  672. { an empty ansi string is nil! }
  673. if (strlength=0) then
  674. curconstSegment.concat(Tai_const.Create_sym(nil))
  675. else
  676. begin
  677. objectlibrary.getdatalabel(ll);
  678. curconstSegment.concat(Tai_const.Create_sym(ll));
  679. consts.concat(tai_align.create(const_align(sizeof(aint))));
  680. consts.concat(Tai_const.Create_aint(-1));
  681. consts.concat(Tai_const.Create_aint(strlength*cwidechartype.def.size));
  682. consts.concat(Tai_label.Create(ll));
  683. for i:=0 to strlength-1 do
  684. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  685. { ending #0 }
  686. Consts.concat(Tai_const.Create_16bit(0))
  687. end;
  688. end;
  689. st_longstring:
  690. begin
  691. internalerror(200107081);
  692. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  693. curconstSegment.concat(Tai_const.Create_8bit(0));
  694. getmem(ca,strlength+1);
  695. move(strval^,ca^,strlength);
  696. ca[strlength]:=#0;
  697. generate_pascii(consts,ca,strlength);
  698. curconstSegment.concat(Tai_const.Create_8bit(0));}
  699. end;
  700. end;
  701. end;
  702. p.free;
  703. end;
  704. arraydef:
  705. begin
  706. { dynamic array nil }
  707. if is_dynamic_array(t.def) then
  708. begin
  709. { Only allow nil initialization }
  710. consume(_NIL);
  711. curconstSegment.concat(Tai_const.Create_sym(nil));
  712. end
  713. else
  714. if try_to_consume(_LKLAMMER) then
  715. begin
  716. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  717. begin
  718. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  719. consume(_COMMA);
  720. end;
  721. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  722. consume(_RKLAMMER);
  723. end
  724. else
  725. { if array of char then we allow also a string }
  726. if is_char(tarraydef(t.def).elementtype.def) then
  727. begin
  728. p:=comp_expr(true);
  729. if p.nodetype=stringconstn then
  730. begin
  731. len:=tstringconstnode(p).len;
  732. { For tp7 the maximum lentgh can be 255 }
  733. if (m_tp7 in aktmodeswitches) and
  734. (len>255) then
  735. len:=255;
  736. ca:=tstringconstnode(p).value_str;
  737. end
  738. else
  739. if is_constcharnode(p) then
  740. begin
  741. c:=chr(tordconstnode(p).value and $ff);
  742. ca:=@c;
  743. len:=1;
  744. end
  745. else
  746. begin
  747. Message(parser_e_illegal_expression);
  748. len:=0;
  749. end;
  750. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  751. Message(parser_e_string_larger_array);
  752. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  753. begin
  754. if i+1-tarraydef(t.def).lowrange<=len then
  755. begin
  756. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  757. inc(ca);
  758. end
  759. else
  760. {Fill the remaining positions with #0.}
  761. curconstSegment.concat(Tai_const.Create_8bit(0));
  762. end;
  763. p.free;
  764. end
  765. else
  766. begin
  767. { we want the ( }
  768. consume(_LKLAMMER);
  769. end;
  770. end;
  771. procvardef:
  772. begin
  773. { Procvars and pointers are no longer compatible. }
  774. { under tp: =nil or =var under fpc: =nil or =@var }
  775. if token=_NIL then
  776. begin
  777. curconstSegment.concat(Tai_const.Create_sym(nil));
  778. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  779. curconstSegment.concat(Tai_const.Create_sym(nil));
  780. consume(_NIL);
  781. goto myexit;
  782. end;
  783. { you can't assign a value other than NIL to a typed constant }
  784. { which is a "procedure of object", because this also requires }
  785. { address of an object/class instance, which is not known at }
  786. { compile time (JM) }
  787. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  788. Message(parser_e_no_procvarobj_const);
  789. { parse the rest too, so we can continue with error checking }
  790. getprocvardef:=tprocvardef(t.def);
  791. p:=comp_expr(true);
  792. getprocvardef:=nil;
  793. if codegenerror then
  794. begin
  795. p.free;
  796. goto myexit;
  797. end;
  798. { let type conversion check everything needed }
  799. inserttypeconv(p,t);
  800. if codegenerror then
  801. begin
  802. p.free;
  803. goto myexit;
  804. end;
  805. { remove typeconvs, that will normally insert a lea
  806. instruction which is not necessary for us }
  807. while p.nodetype=typeconvn do
  808. begin
  809. hp:=ttypeconvnode(p).left;
  810. ttypeconvnode(p).left:=nil;
  811. p.free;
  812. p:=hp;
  813. end;
  814. { remove addrn which we also don't need here }
  815. if p.nodetype=addrn then
  816. begin
  817. hp:=taddrnode(p).left;
  818. taddrnode(p).left:=nil;
  819. p.free;
  820. p:=hp;
  821. end;
  822. { we now need to have a loadn with a procsym }
  823. if (p.nodetype=loadn) and
  824. (tloadnode(p).symtableentry.typ=procsym) then
  825. begin
  826. curconstSegment.concat(Tai_const.createname(
  827. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,AT_FUNCTION,0));
  828. end
  829. else
  830. Message(parser_e_illegal_expression);
  831. p.free;
  832. end;
  833. { reads a typed constant record }
  834. recorddef:
  835. begin
  836. { KAZ }
  837. if (trecorddef(t.def)=rec_tguid) and
  838. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  839. begin
  840. p:=comp_expr(true);
  841. inserttypeconv(p,cshortstringtype);
  842. if p.nodetype=stringconstn then
  843. begin
  844. s:=strpas(tstringconstnode(p).value_str);
  845. p.free;
  846. if string2guid(s,tmpguid) then
  847. begin
  848. curconstSegment.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  849. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  850. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  851. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  852. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  853. end
  854. else
  855. Message(parser_e_improper_guid_syntax);
  856. end
  857. else
  858. begin
  859. p.free;
  860. Message(parser_e_illegal_expression);
  861. goto myexit;
  862. end;
  863. end
  864. else
  865. begin
  866. consume(_LKLAMMER);
  867. sorg:='';
  868. aktpos:=0;
  869. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  870. recsym := nil;
  871. while token<>_RKLAMMER do
  872. begin
  873. s:=pattern;
  874. sorg:=orgpattern;
  875. consume(_ID);
  876. consume(_COLON);
  877. error := false;
  878. recsym := tsym(trecorddef(t.def).symtable.search(s));
  879. if not assigned(recsym) then
  880. begin
  881. Message1(sym_e_illegal_field,sorg);
  882. error := true;
  883. end;
  884. if (not error) and
  885. (not assigned(srsym) or
  886. (s <> srsym.name)) then
  887. { possible variant record (JM) }
  888. begin
  889. { All parts of a variant start at the same offset }
  890. { Also allow jumping from one variant part to another, }
  891. { as long as the offsets match }
  892. if (assigned(srsym) and
  893. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  894. { srsym is not assigned after parsing w2 in the }
  895. { typed const in the next example: }
  896. { type tr = record case byte of }
  897. { 1: (l1,l2: dword); }
  898. { 2: (w1,w2: word); }
  899. { end; }
  900. { const r: tr = (w1:1;w2:1;l2:5); }
  901. (tfieldvarsym(recsym).fieldoffset = aktpos) then
  902. srsym := recsym
  903. { going backwards isn't allowed in any mode }
  904. else if (tfieldvarsym(recsym).fieldoffset<aktpos) then
  905. begin
  906. Message(parser_e_invalid_record_const);
  907. error := true;
  908. end
  909. { Delphi allows you to skip fields }
  910. else if (m_delphi in aktmodeswitches) then
  911. begin
  912. Message1(parser_w_skipped_fields_before,sorg);
  913. srsym := recsym;
  914. end
  915. { FPC and TP don't }
  916. else
  917. begin
  918. Message1(parser_e_skipped_fields_before,sorg);
  919. error := true;
  920. end;
  921. end;
  922. if error then
  923. consume_all_until(_SEMICOLON)
  924. else
  925. begin
  926. { if needed fill (alignment) }
  927. if tfieldvarsym(srsym).fieldoffset>aktpos then
  928. for i:=1 to tfieldvarsym(srsym).fieldoffset-aktpos do
  929. curconstSegment.concat(Tai_const.Create_8bit(0));
  930. { new position }
  931. aktpos:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vartype.def.size;
  932. { read the data }
  933. readtypedconst(tfieldvarsym(srsym).vartype,nil,writable);
  934. { keep previous field for checking whether whole }
  935. { record was initialized (JM) }
  936. recsym := srsym;
  937. { goto next field }
  938. srsym := tsym(srsym.indexnext);
  939. if token=_SEMICOLON then
  940. consume(_SEMICOLON)
  941. else break;
  942. end;
  943. end;
  944. { are there any fields left? }
  945. if assigned(srsym) and
  946. { don't complain if there only come other variant parts }
  947. { after the last initialized field }
  948. ((recsym=nil) or
  949. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)) then
  950. Message1(parser_w_skipped_fields_after,sorg);
  951. for i:=1 to t.def.size-aktpos do
  952. curconstSegment.concat(Tai_const.Create_8bit(0));
  953. consume(_RKLAMMER);
  954. end;
  955. end;
  956. { reads a typed object }
  957. objectdef:
  958. begin
  959. if is_class_or_interface(t.def) then
  960. begin
  961. p:=comp_expr(true);
  962. if p.nodetype<>niln then
  963. begin
  964. Message(parser_e_type_const_not_possible);
  965. consume_all_until(_RKLAMMER);
  966. end
  967. else
  968. begin
  969. curconstSegment.concat(Tai_const.Create_sym(nil));
  970. end;
  971. p.free;
  972. end
  973. { for objects we allow it only if it doesn't contain a vmt }
  974. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  975. (m_fpc in aktmodeswitches) then
  976. Message(parser_e_type_const_not_possible)
  977. else
  978. begin
  979. consume(_LKLAMMER);
  980. aktpos:=0;
  981. while token<>_RKLAMMER do
  982. begin
  983. s:=pattern;
  984. sorg:=orgpattern;
  985. consume(_ID);
  986. consume(_COLON);
  987. srsym:=nil;
  988. obj:=tobjectdef(t.def);
  989. symt:=obj.symtable;
  990. while (srsym=nil) and assigned(symt) do
  991. begin
  992. srsym:=tsym(symt.search(s));
  993. if assigned(obj) then
  994. obj:=obj.childof;
  995. if assigned(obj) then
  996. symt:=obj.symtable
  997. else
  998. symt:=nil;
  999. end;
  1000. if srsym=nil then
  1001. begin
  1002. Message1(sym_e_id_not_found,sorg);
  1003. consume_all_until(_SEMICOLON);
  1004. end
  1005. else
  1006. with tfieldvarsym(srsym) do
  1007. begin
  1008. { check position }
  1009. if fieldoffset<aktpos then
  1010. message(parser_e_invalid_record_const);
  1011. { check in VMT needs to be added for TP mode }
  1012. with Tobjectdef(t.def) do
  1013. if not(m_fpc in aktmodeswitches) and
  1014. (oo_has_vmt in objectoptions) and
  1015. (vmt_offset<fieldoffset) then
  1016. begin
  1017. for i:=1 to vmt_offset-aktpos do
  1018. curconstsegment.concat(tai_const.create_8bit(0));
  1019. curconstsegment.concat(tai_const.createname(vmt_mangledname,AT_DATA,0));
  1020. { this is more general }
  1021. aktpos:=vmt_offset + sizeof(aint);
  1022. end;
  1023. { if needed fill }
  1024. if fieldoffset>aktpos then
  1025. for i:=1 to fieldoffset-aktpos do
  1026. curconstSegment.concat(Tai_const.Create_8bit(0));
  1027. { new position }
  1028. aktpos:=fieldoffset+vartype.def.size;
  1029. { read the data }
  1030. readtypedconst(vartype,nil,writable);
  1031. if token=_SEMICOLON then
  1032. consume(_SEMICOLON)
  1033. else break;
  1034. end;
  1035. end;
  1036. if not(m_fpc in aktmodeswitches) and
  1037. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  1038. (tobjectdef(t.def).vmt_offset>=aktpos) then
  1039. begin
  1040. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  1041. curconstsegment.concat(tai_const.create_8bit(0));
  1042. curconstsegment.concat(tai_const.createname(tobjectdef(t.def).vmt_mangledname,AT_DATA,0));
  1043. { this is more general }
  1044. aktpos:=tobjectdef(t.def).vmt_offset + sizeof(aint);
  1045. end;
  1046. for i:=1 to t.def.size-aktpos do
  1047. curconstSegment.concat(Tai_const.Create_8bit(0));
  1048. consume(_RKLAMMER);
  1049. end;
  1050. end;
  1051. errordef:
  1052. begin
  1053. { try to consume something useful }
  1054. if token=_LKLAMMER then
  1055. consume_all_until(_RKLAMMER)
  1056. else
  1057. consume_all_until(_SEMICOLON);
  1058. end;
  1059. else Message(parser_e_type_const_not_possible);
  1060. end;
  1061. myexit:
  1062. block_type:=old_block_type;
  1063. if assigned(sym) then
  1064. begin
  1065. storefilepos:=aktfilepos;
  1066. aktfilepos:=sym.fileinfo;
  1067. curconstSegment.concat(tai_symbol_end.Createname(sym.mangledname));
  1068. aktfilepos:=storefilepos;
  1069. end;
  1070. end;
  1071. {$ifdef fpc}
  1072. {$maxfpuregisters default}
  1073. {$endif fpc}
  1074. end.