ptconst.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Reads typed constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptconst;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses symtype,symsym;
  22. { this procedure reads typed constants }
  23. { sym is only needed for ansi strings }
  24. { the assembler label is in the middle (PM) }
  25. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  26. implementation
  27. uses
  28. {$ifdef Delphi}
  29. sysutils,
  30. {$else}
  31. strings,
  32. {$endif Delphi}
  33. globtype,systems,tokens,verbose,
  34. cutils,globals,widestr,scanner,
  35. symconst,symbase,symdef,symtable,
  36. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  37. { pass 1 }
  38. node,
  39. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser specific stuff }
  41. pbase,pexpr,
  42. { codegen }
  43. cpuinfo,cgbase
  44. ;
  45. {$ifdef fpc}
  46. {$maxfpuregisters 0}
  47. {$endif fpc}
  48. { this procedure reads typed constants }
  49. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  50. type
  51. setbytes = array[0..31] of byte;
  52. Psetbytes = ^setbytes;
  53. var
  54. len,base : longint;
  55. p,hp,hpstart : tnode;
  56. i,j,l,offset,
  57. varalign,
  58. strlength : longint;
  59. curconstsegment : TAAsmoutput;
  60. ll : tasmlabel;
  61. s,sorg : string;
  62. c : char;
  63. ca : pchar;
  64. tmpguid : tguid;
  65. aktpos : longint;
  66. obj : tobjectdef;
  67. recsym,
  68. srsym : tsym;
  69. symt : tsymtable;
  70. value : bestreal;
  71. intvalue : tconstexprint;
  72. strval : pchar;
  73. pw : pcompilerwidestring;
  74. error : boolean;
  75. old_block_type : tblock_type;
  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. {$R-} {Range check creates problem with init_8bit(-1) !!}
  88. begin
  89. old_block_type:=block_type;
  90. block_type:=bt_const;
  91. if writable then
  92. curconstsegment:=datasegment
  93. else
  94. curconstsegment:=consts;
  95. case t.def.deftype of
  96. orddef:
  97. begin
  98. p:=comp_expr(true);
  99. case torddef(t.def).typ of
  100. bool8bit :
  101. begin
  102. if is_constboolnode(p) then
  103. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  104. else
  105. Message(cg_e_illegal_expression);
  106. end;
  107. bool16bit :
  108. begin
  109. if is_constboolnode(p) then
  110. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  111. else
  112. Message(cg_e_illegal_expression);
  113. end;
  114. bool32bit :
  115. begin
  116. if is_constboolnode(p) then
  117. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value))
  118. else
  119. Message(cg_e_illegal_expression);
  120. end;
  121. uchar :
  122. begin
  123. if is_constcharnode(p) then
  124. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  125. else
  126. Message(cg_e_illegal_expression);
  127. end;
  128. uwidechar :
  129. begin
  130. if is_constcharnode(p) then
  131. inserttypeconv(p,cwidechartype);
  132. if is_constwidecharnode(p) then
  133. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  134. else
  135. Message(cg_e_illegal_expression);
  136. end;
  137. s8bit,
  138. u8bit :
  139. begin
  140. if is_constintnode(p) then
  141. begin
  142. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  143. check_range(torddef(t.def));
  144. end
  145. else
  146. Message(cg_e_illegal_expression);
  147. end;
  148. u16bit,
  149. s16bit :
  150. begin
  151. if is_constintnode(p) then
  152. begin
  153. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  154. check_range(torddef(t.def));
  155. end
  156. else
  157. Message(cg_e_illegal_expression);
  158. end;
  159. s32bit,
  160. u32bit :
  161. begin
  162. if is_constintnode(p) then
  163. begin
  164. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  165. if torddef(t.def).typ<>u32bit then
  166. check_range(torddef(t.def));
  167. end
  168. else
  169. Message(cg_e_illegal_expression);
  170. end;
  171. s64bit,
  172. u64bit,
  173. scurrency:
  174. begin
  175. if is_constintnode(p) then
  176. intvalue := tordconstnode(p).value
  177. {$ifndef VER1_0}
  178. else if is_constrealnode(p) and
  179. (torddef(t.def).typ = scurrency) and
  180. (trealconstnode(p).value_real*10000 >= low(int64)) and
  181. (trealconstnode(p).value_real*10000 <= high(int64)) then
  182. intvalue := round(trealconstnode(p).value_real*10000)
  183. {$endif ndef VER1_0}
  184. else
  185. begin
  186. intvalue := 0;
  187. Message(cg_e_illegal_expression);
  188. end;
  189. {$warning maybe change to create_64bit}
  190. if target_info.endian = endian_little then
  191. begin
  192. curconstSegment.concat(Tai_const.Create_32bit(Cardinal(intvalue and $ffffffff)));
  193. curconstSegment.concat(Tai_const.Create_32bit(Cardinal(intvalue shr 32)));
  194. end
  195. else
  196. begin
  197. curconstSegment.concat(Tai_const.Create_32bit(Cardinal(intvalue shr 32)));
  198. curconstSegment.concat(Tai_const.Create_32bit(Cardinal(intvalue and $ffffffff)));
  199. end;
  200. end;
  201. else
  202. internalerror(3799);
  203. end;
  204. p.free;
  205. end;
  206. floatdef:
  207. begin
  208. p:=comp_expr(true);
  209. if is_constrealnode(p) then
  210. value:=trealconstnode(p).value_real
  211. else if is_constintnode(p) then
  212. value:=tordconstnode(p).value
  213. else
  214. Message(cg_e_illegal_expression);
  215. case tfloatdef(t.def).typ of
  216. s32real :
  217. curconstSegment.concat(Tai_real_32bit.Create(ts32real(value)));
  218. s64real :
  219. {$ifdef ARM}
  220. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  221. curconstSegment.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  222. else
  223. {$endif ARM}
  224. curconstSegment.concat(Tai_real_64bit.Create(ts64real(value)));
  225. s80real :
  226. curconstSegment.concat(Tai_real_80bit.Create(value));
  227. {$ifdef ver1_0}
  228. s64comp :
  229. curconstSegment.concat(Tai_comp_64bit.Create(value));
  230. s64currency:
  231. curconstSegment.concat(Tai_comp_64bit.Create(value*10000));
  232. {$else ver1_0}
  233. { the round is necessary for native compilers where comp isn't a float }
  234. s64comp :
  235. curconstSegment.concat(Tai_comp_64bit.Create(round(value)));
  236. s64currency:
  237. curconstSegment.concat(Tai_comp_64bit.Create(round(value*10000)));
  238. {$endif ver1_0}
  239. s128real:
  240. curconstSegment.concat(Tai_real_128bit.Create(value));
  241. else
  242. internalerror(18);
  243. end;
  244. p.free;
  245. end;
  246. classrefdef:
  247. begin
  248. p:=comp_expr(true);
  249. case p.nodetype of
  250. loadvmtaddrn:
  251. with Tclassrefdef(p.resulttype.def) do
  252. begin
  253. if not Tobjectdef(pointertype.def).is_related(Tobjectdef(pointertype.def)) then
  254. message(cg_e_illegal_expression);
  255. curconstSegment.concat(Tai_const_symbol.Create(objectlibrary.newasmsymbol(
  256. Tobjectdef(pointertype.def).vmt_mangledname,AB_EXTERNAL,AT_DATA)));
  257. end;
  258. niln:
  259. curconstSegment.concat(Tai_const.Create_ptr(0));
  260. else Message(cg_e_illegal_expression);
  261. end;
  262. p.free;
  263. end;
  264. pointerdef:
  265. begin
  266. p:=comp_expr(true);
  267. if (p.nodetype=typeconvn) then
  268. with Ttypeconvnode(p) do
  269. if (left.nodetype in [addrn,niln]) and equal_defs(t.def,p.resulttype.def) then
  270. begin
  271. hp:=left;
  272. left:=nil;
  273. p.free;
  274. p:=hp;
  275. end;
  276. { allows horrible ofs(typeof(TButton)^) code !! }
  277. if (p.nodetype=addrn) then
  278. with Taddrnode(p) do
  279. if left.nodetype=derefn then
  280. begin
  281. hp:=tderefnode(left).left;
  282. tderefnode(left).left:=nil;
  283. p.free;
  284. p:=hp;
  285. end;
  286. { const pointer ? }
  287. if (p.nodetype = pointerconstn) then
  288. curconstsegment.concat(Tai_const.Create_ptr(TConstPtrUInt(tpointerconstnode(p).value)))
  289. { nil pointer ? }
  290. else if p.nodetype=niln then
  291. curconstSegment.concat(Tai_const.Create_ptr(0))
  292. { maybe pchar ? }
  293. else
  294. if is_char(tpointerdef(t.def).pointertype.def) and
  295. (p.nodetype<>addrn) then
  296. begin
  297. objectlibrary.getdatalabel(ll);
  298. curconstSegment.concat(Tai_const_symbol.Create(ll));
  299. if p.nodetype=stringconstn then
  300. varalign:=tstringconstnode(p).len
  301. else
  302. varalign:=0;
  303. varalign:=const_align(varalign);
  304. Consts.concat(Tai_align.Create(varalign));
  305. Consts.concat(Tai_label.Create(ll));
  306. if p.nodetype=stringconstn then
  307. begin
  308. len:=tstringconstnode(p).len;
  309. { For tp7 the maximum lentgh can be 255 }
  310. if (m_tp7 in aktmodeswitches) and
  311. (len>255) then
  312. len:=255;
  313. getmem(ca,len+2);
  314. move(tstringconstnode(p).value_str^,ca^,len+1);
  315. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  316. end
  317. else
  318. if is_constcharnode(p) then
  319. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  320. else
  321. Message(cg_e_illegal_expression);
  322. end
  323. { maybe pwidechar ? }
  324. else
  325. if is_widechar(tpointerdef(t.def).pointertype.def) and
  326. (p.nodetype<>addrn) then
  327. begin
  328. objectlibrary.getdatalabel(ll);
  329. curconstSegment.concat(Tai_const_symbol.Create(ll));
  330. Consts.concat(tai_align.create(const_align(pointer_size)));
  331. Consts.concat(Tai_label.Create(ll));
  332. if (p.nodetype in [stringconstn,ordconstn]) then
  333. begin
  334. { convert to widestring stringconstn }
  335. inserttypeconv(p,cwidestringtype);
  336. if (p.nodetype=stringconstn) and
  337. (tstringconstnode(p).st_type=st_widestring) then
  338. begin
  339. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  340. for i:=0 to tstringconstnode(p).len-1 do
  341. Consts.concat(Tai_const.Create_16bit(pw^.data[i]));
  342. { ending #0 }
  343. Consts.concat(Tai_const.Create_16bit(0))
  344. end;
  345. end
  346. else
  347. Message(cg_e_illegal_expression);
  348. end
  349. else
  350. if p.nodetype=addrn then
  351. begin
  352. { support @@procvar in tp mode }
  353. if (m_tp_procvar in aktmodeswitches) and
  354. (taddrnode(p).left.nodetype=addrn) then
  355. p:=taddrnode(p).left;
  356. { insert typeconv }
  357. inserttypeconv(p,t);
  358. { if a typeconv node was inserted then check if it was an tc_equal. If
  359. true then we remove the node. If not tc_equal then we leave the typeconvn
  360. and the nodetype=loadn will always be false and generate the error (PFV) }
  361. if (p.nodetype=typeconvn) then
  362. begin
  363. if (ttypeconvnode(p).convtype=tc_equal) then
  364. hpstart:=taddrnode(ttypeconvnode(p).left).left
  365. else
  366. hpstart:=p;
  367. end
  368. else
  369. hpstart:=taddrnode(p).left;
  370. hp:=hpstart;
  371. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  372. hp:=tunarynode(hp).left;
  373. if (hp.nodetype=loadn) then
  374. begin
  375. hp:=hpstart;
  376. offset:=0;
  377. while assigned(hp) and (hp.nodetype<>loadn) do
  378. begin
  379. case hp.nodetype of
  380. vecn :
  381. begin
  382. case tvecnode(hp).left.resulttype.def.deftype of
  383. stringdef :
  384. begin
  385. { this seems OK for shortstring and ansistrings PM }
  386. { it is wrong for widestrings !! }
  387. len:=1;
  388. base:=0;
  389. end;
  390. arraydef :
  391. begin
  392. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  393. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  394. end
  395. else
  396. Message(cg_e_illegal_expression);
  397. end;
  398. if is_constintnode(tvecnode(hp).right) then
  399. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  400. else
  401. Message(cg_e_illegal_expression);
  402. end;
  403. subscriptn :
  404. inc(offset,tsubscriptnode(hp).vs.fieldoffset)
  405. else
  406. Message(cg_e_illegal_expression);
  407. end;
  408. hp:=tbinarynode(hp).left;
  409. end;
  410. srsym:=tloadnode(hp).symtableentry;
  411. case srsym.typ of
  412. procsym :
  413. begin
  414. if Tprocsym(srsym).procdef_count>1 then
  415. Message(parser_e_no_overloaded_procvars);
  416. if po_abstractmethod in tprocsym(srsym).first_procdef.procoptions then
  417. Message(type_e_cant_take_address_of_abstract_method)
  418. else
  419. curconstSegment.concat(Tai_const_symbol.Createname(tprocsym(srsym).first_procdef.mangledname,AT_FUNCTION,offset));
  420. end;
  421. varsym :
  422. curconstSegment.concat(Tai_const_symbol.Createname(tvarsym(srsym).mangledname,AT_DATA,offset));
  423. typedconstsym :
  424. curconstSegment.concat(Tai_const_symbol.Createname(ttypedconstsym(srsym).mangledname,AT_DATA,offset));
  425. else
  426. Message(type_e_variable_id_expected);
  427. end;
  428. end
  429. else
  430. Message(cg_e_illegal_expression);
  431. end
  432. else
  433. { allow typeof(Object type)}
  434. if (p.nodetype=inlinen) and
  435. (tinlinenode(p).inlinenumber=in_typeof_x) then
  436. begin
  437. if (tinlinenode(p).left.nodetype=typen) then
  438. begin
  439. curconstSegment.concat(Tai_const_symbol.createname(
  440. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname,AT_DATA,0));
  441. end
  442. else
  443. Message(cg_e_illegal_expression);
  444. end
  445. else
  446. Message(cg_e_illegal_expression);
  447. p.free;
  448. end;
  449. setdef:
  450. begin
  451. p:=comp_expr(true);
  452. if p.nodetype=setconstn then
  453. begin
  454. { be sure to convert to the correct result, else
  455. it can generate smallset data instead of normalset (PFV) }
  456. inserttypeconv(p,t);
  457. { we only allow const sets }
  458. if assigned(tsetconstnode(p).left) then
  459. Message(cg_e_illegal_expression)
  460. else
  461. begin
  462. { this writing is endian independant }
  463. { untrue - because they are considered }
  464. { arrays of 32-bit values CEC }
  465. if source_info.endian = target_info.endian then
  466. begin
  467. for l:=0 to p.resulttype.def.size-1 do
  468. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[l]));
  469. end
  470. else
  471. begin
  472. { store as longint values in swaped format }
  473. j:=0;
  474. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  475. begin
  476. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  477. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  478. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  479. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  480. Inc(j,4);
  481. end;
  482. end;
  483. end;
  484. end
  485. else
  486. Message(cg_e_illegal_expression);
  487. p.free;
  488. end;
  489. enumdef:
  490. begin
  491. p:=comp_expr(true);
  492. if p.nodetype=ordconstn then
  493. begin
  494. if equal_defs(p.resulttype.def,t.def) or
  495. is_subequal(p.resulttype.def,t.def) then
  496. begin
  497. case p.resulttype.def.size of
  498. 1 : curconstSegment.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  499. 2 : curconstSegment.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  500. 4 : curconstSegment.concat(Tai_const.Create_32bit(Cardinal(tordconstnode(p).value)));
  501. end;
  502. end
  503. else
  504. IncompatibleTypes(p.resulttype.def,t.def);
  505. end
  506. else
  507. Message(cg_e_illegal_expression);
  508. p.free;
  509. end;
  510. stringdef:
  511. begin
  512. p:=comp_expr(true);
  513. { load strval and strlength of the constant tree }
  514. if (p.nodetype=stringconstn) or is_widestring(t.def) then
  515. begin
  516. { convert to the expected string type so that
  517. for widestrings strval is a pcompilerwidestring }
  518. inserttypeconv(p,t);
  519. strlength:=tstringconstnode(p).len;
  520. strval:=tstringconstnode(p).value_str;
  521. end
  522. else if is_constcharnode(p) then
  523. begin
  524. { strval:=pchar(@tordconstnode(p).value);
  525. THIS FAIL on BIG_ENDIAN MACHINES PM }
  526. c:=chr(tordconstnode(p).value and $ff);
  527. strval:=@c;
  528. strlength:=1
  529. end
  530. else if is_constresourcestringnode(p) then
  531. begin
  532. strval:=pchar(tconstsym(tloadnode(p).symtableentry).value.valueptr);
  533. strlength:=tconstsym(tloadnode(p).symtableentry).value.len;
  534. end
  535. else
  536. begin
  537. Message(cg_e_illegal_expression);
  538. strlength:=-1;
  539. end;
  540. if strlength>=0 then
  541. begin
  542. case tstringdef(t.def).string_typ of
  543. st_shortstring:
  544. begin
  545. if strlength>=t.def.size then
  546. begin
  547. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  548. strlength:=t.def.size-1;
  549. end;
  550. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  551. { this can also handle longer strings }
  552. getmem(ca,strlength+1);
  553. move(strval^,ca^,strlength);
  554. ca[strlength]:=#0;
  555. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  556. { fillup with spaces if size is shorter }
  557. if t.def.size>strlength then
  558. begin
  559. getmem(ca,t.def.size-strlength);
  560. { def.size contains also the leading length, so we }
  561. { we have to subtract one }
  562. fillchar(ca[0],t.def.size-strlength-1,' ');
  563. ca[t.def.size-strlength-1]:=#0;
  564. { this can also handle longer strings }
  565. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  566. end;
  567. end;
  568. st_ansistring:
  569. begin
  570. { an empty ansi string is nil! }
  571. if (strlength=0) then
  572. curconstSegment.concat(Tai_const.Create_ptr(0))
  573. else
  574. begin
  575. objectlibrary.getdatalabel(ll);
  576. curconstSegment.concat(Tai_const_symbol.Create(ll));
  577. { the actual structure starts at -12 from start label - CEC }
  578. Consts.concat(tai_align.create(const_align(pointer_size)));
  579. { first write the maximum size }
  580. Consts.concat(Tai_const.Create_32bit(strlength));
  581. { second write the real length }
  582. Consts.concat(Tai_const.Create_32bit(strlength));
  583. { redondent with maxlength but who knows ... (PM) }
  584. { third write use count (set to -1 for safety ) }
  585. Consts.concat(Tai_const.Create_32bit(Cardinal(-1)));
  586. Consts.concat(Tai_label.Create(ll));
  587. getmem(ca,strlength+2);
  588. move(strval^,ca^,strlength);
  589. { The terminating #0 to be stored in the .data section (JM) }
  590. ca[strlength]:=#0;
  591. { End of the PChar. The memory has to be allocated because in }
  592. { tai_string.done, there is a freemem(len+1) (JM) }
  593. ca[strlength+1]:=#0;
  594. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  595. end;
  596. end;
  597. st_widestring:
  598. begin
  599. { an empty ansi string is nil! }
  600. if (strlength=0) then
  601. curconstSegment.concat(Tai_const.Create_ptr(0))
  602. else
  603. begin
  604. objectlibrary.getdatalabel(ll);
  605. curconstSegment.concat(Tai_const_symbol.Create(ll));
  606. { the actual structure starts at -12 from start label - CEC }
  607. Consts.concat(tai_align.create(const_align(pointer_size)));
  608. Consts.concat(Tai_const.Create_32bit(strlength));
  609. Consts.concat(Tai_const.Create_32bit(strlength));
  610. Consts.concat(Tai_const.Create_32bit(Cardinal(-1)));
  611. Consts.concat(Tai_label.Create(ll));
  612. for i:=0 to strlength-1 do
  613. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  614. { ending #0 }
  615. Consts.concat(Tai_const.Create_16bit(0))
  616. end;
  617. end;
  618. st_longstring:
  619. begin
  620. internalerror(200107081);
  621. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  622. curconstSegment.concat(Tai_const.Create_8bit(0));
  623. getmem(ca,strlength+1);
  624. move(strval^,ca^,strlength);
  625. ca[strlength]:=#0;
  626. generate_pascii(consts,ca,strlength);
  627. curconstSegment.concat(Tai_const.Create_8bit(0));}
  628. end;
  629. end;
  630. end;
  631. p.free;
  632. end;
  633. arraydef:
  634. begin
  635. if try_to_consume(_LKLAMMER) then
  636. begin
  637. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  638. begin
  639. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  640. consume(_COMMA);
  641. end;
  642. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  643. consume(_RKLAMMER);
  644. end
  645. else
  646. { if array of char then we allow also a string }
  647. if is_char(tarraydef(t.def).elementtype.def) then
  648. begin
  649. p:=comp_expr(true);
  650. if p.nodetype=stringconstn then
  651. begin
  652. len:=tstringconstnode(p).len;
  653. { For tp7 the maximum lentgh can be 255 }
  654. if (m_tp7 in aktmodeswitches) and
  655. (len>255) then
  656. len:=255;
  657. ca:=tstringconstnode(p).value_str;
  658. end
  659. else
  660. if is_constcharnode(p) then
  661. begin
  662. c:=chr(tordconstnode(p).value and $ff);
  663. ca:=@c;
  664. len:=1;
  665. end
  666. else
  667. begin
  668. Message(cg_e_illegal_expression);
  669. len:=0;
  670. end;
  671. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  672. Message(parser_e_string_larger_array);
  673. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  674. begin
  675. if i+1-tarraydef(t.def).lowrange<=len then
  676. begin
  677. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  678. inc(ca);
  679. end
  680. else
  681. {Fill the remaining positions with #0.}
  682. curconstSegment.concat(Tai_const.Create_8bit(0));
  683. end;
  684. p.free;
  685. end
  686. else
  687. { dynamic array nil }
  688. if is_dynamic_array(t.def) then
  689. begin
  690. { Only allow nil initialization }
  691. consume(_NIL);
  692. curconstSegment.concat(Tai_const.Create_ptr(0));
  693. end
  694. else
  695. begin
  696. { we want the ( }
  697. consume(_LKLAMMER);
  698. end;
  699. end;
  700. procvardef:
  701. begin
  702. { Procvars and pointers are no longer compatible. }
  703. { under tp: =nil or =var under fpc: =nil or =@var }
  704. if token=_NIL then
  705. begin
  706. curconstSegment.concat(Tai_const.Create_ptr(0));
  707. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  708. curconstSegment.concat(Tai_const.Create_ptr(0));
  709. consume(_NIL);
  710. exit;
  711. end;
  712. { you can't assign a value other than NIL to a typed constant }
  713. { which is a "procedure of object", because this also requires }
  714. { address of an object/class instance, which is not known at }
  715. { compile time (JM) }
  716. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  717. Message(parser_e_no_procvarobj_const);
  718. { parse the rest too, so we can continue with error checking }
  719. getprocvardef:=tprocvardef(t.def);
  720. p:=comp_expr(true);
  721. getprocvardef:=nil;
  722. if codegenerror then
  723. begin
  724. p.free;
  725. exit;
  726. end;
  727. { let type conversion check everything needed }
  728. inserttypeconv(p,t);
  729. if codegenerror then
  730. begin
  731. p.free;
  732. exit;
  733. end;
  734. { remove typeconvn, that will normally insert a lea
  735. instruction which is not necessary for us }
  736. if p.nodetype=typeconvn then
  737. begin
  738. hp:=ttypeconvnode(p).left;
  739. ttypeconvnode(p).left:=nil;
  740. p.free;
  741. p:=hp;
  742. end;
  743. { remove addrn which we also don't need here }
  744. if p.nodetype=addrn then
  745. begin
  746. hp:=taddrnode(p).left;
  747. taddrnode(p).left:=nil;
  748. p.free;
  749. p:=hp;
  750. end;
  751. { we now need to have a loadn with a procsym }
  752. if (p.nodetype=loadn) and
  753. (tloadnode(p).symtableentry.typ=procsym) then
  754. begin
  755. curconstSegment.concat(Tai_const_symbol.createname(
  756. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,AT_FUNCTION,0));
  757. end
  758. else
  759. Message(cg_e_illegal_expression);
  760. p.free;
  761. end;
  762. { reads a typed constant record }
  763. recorddef:
  764. begin
  765. { KAZ }
  766. if (trecorddef(t.def)=rec_tguid) and
  767. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  768. begin
  769. p:=comp_expr(true);
  770. inserttypeconv(p,cshortstringtype);
  771. if p.nodetype=stringconstn then
  772. begin
  773. s:=strpas(tstringconstnode(p).value_str);
  774. p.free;
  775. if string2guid(s,tmpguid) then
  776. begin
  777. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  778. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  779. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  780. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  781. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  782. end
  783. else
  784. Message(parser_e_improper_guid_syntax);
  785. end
  786. else
  787. begin
  788. p.free;
  789. Message(cg_e_illegal_expression);
  790. exit;
  791. end;
  792. end
  793. else
  794. begin
  795. consume(_LKLAMMER);
  796. sorg:='';
  797. aktpos:=0;
  798. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  799. recsym := nil;
  800. while token<>_RKLAMMER do
  801. begin
  802. s:=pattern;
  803. sorg:=orgpattern;
  804. consume(_ID);
  805. consume(_COLON);
  806. error := false;
  807. recsym := tsym(trecorddef(t.def).symtable.search(s));
  808. if not assigned(recsym) then
  809. begin
  810. Message1(sym_e_illegal_field,sorg);
  811. error := true;
  812. end;
  813. if (not error) and
  814. (not assigned(srsym) or
  815. (s <> srsym.name)) then
  816. { possible variant record (JM) }
  817. begin
  818. { All parts of a variant start at the same offset }
  819. { Also allow jumping from one variant part to another, }
  820. { as long as the offsets match }
  821. if (assigned(srsym) and
  822. (tvarsym(recsym).fieldoffset = tvarsym(srsym).fieldoffset)) or
  823. { srsym is not assigned after parsing w2 in the }
  824. { typed const in the next example: }
  825. { type tr = record case byte of }
  826. { 1: (l1,l2: dword); }
  827. { 2: (w1,w2: word); }
  828. { end; }
  829. { const r: tr = (w1:1;w2:1;l2:5); }
  830. (tvarsym(recsym).fieldoffset = aktpos) then
  831. srsym := recsym
  832. { going backwards isn't allowed in any mode }
  833. else if (tvarsym(recsym).fieldoffset<aktpos) then
  834. begin
  835. Message(parser_e_invalid_record_const);
  836. error := true;
  837. end
  838. { Delphi allows you to skip fields }
  839. else if (m_delphi in aktmodeswitches) then
  840. begin
  841. Message1(parser_w_skipped_fields_before,sorg);
  842. srsym := recsym;
  843. end
  844. { FPC and TP don't }
  845. else
  846. begin
  847. Message1(parser_e_skipped_fields_before,sorg);
  848. error := true;
  849. end;
  850. end;
  851. if error then
  852. consume_all_until(_SEMICOLON)
  853. else
  854. begin
  855. { if needed fill (alignment) }
  856. if tvarsym(srsym).fieldoffset>aktpos then
  857. for i:=1 to tvarsym(srsym).fieldoffset-aktpos do
  858. curconstSegment.concat(Tai_const.Create_8bit(0));
  859. { new position }
  860. aktpos:=tvarsym(srsym).fieldoffset+tvarsym(srsym).vartype.def.size;
  861. { read the data }
  862. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  863. { keep previous field for checking whether whole }
  864. { record was initialized (JM) }
  865. recsym := srsym;
  866. { goto next field }
  867. srsym := tsym(srsym.indexnext);
  868. if token=_SEMICOLON then
  869. consume(_SEMICOLON)
  870. else break;
  871. end;
  872. end;
  873. { are there any fields left? }
  874. if assigned(srsym) and
  875. { don't complain if there only come other variant parts }
  876. { after the last initialized field }
  877. ((recsym=nil) or
  878. (tvarsym(srsym).fieldoffset > tvarsym(recsym).fieldoffset)) then
  879. Message1(parser_w_skipped_fields_after,sorg);
  880. for i:=1 to t.def.size-aktpos do
  881. curconstSegment.concat(Tai_const.Create_8bit(0));
  882. consume(_RKLAMMER);
  883. end;
  884. end;
  885. { reads a typed object }
  886. objectdef:
  887. begin
  888. if is_class_or_interface(t.def) then
  889. begin
  890. p:=comp_expr(true);
  891. if p.nodetype<>niln then
  892. begin
  893. Message(parser_e_type_const_not_possible);
  894. consume_all_until(_RKLAMMER);
  895. end
  896. else
  897. begin
  898. curconstSegment.concat(Tai_const.Create_ptr(0));
  899. end;
  900. p.free;
  901. end
  902. { for objects we allow it only if it doesn't contain a vmt }
  903. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  904. (m_fpc in aktmodeswitches) then
  905. Message(parser_e_type_const_not_possible)
  906. else
  907. begin
  908. consume(_LKLAMMER);
  909. aktpos:=0;
  910. while token<>_RKLAMMER do
  911. begin
  912. s:=pattern;
  913. sorg:=orgpattern;
  914. consume(_ID);
  915. consume(_COLON);
  916. srsym:=nil;
  917. obj:=tobjectdef(t.def);
  918. symt:=obj.symtable;
  919. while (srsym=nil) and assigned(symt) do
  920. begin
  921. srsym:=tsym(symt.search(s));
  922. if assigned(obj) then
  923. obj:=obj.childof;
  924. if assigned(obj) then
  925. symt:=obj.symtable
  926. else
  927. symt:=nil;
  928. end;
  929. if srsym=nil then
  930. begin
  931. Message1(sym_e_id_not_found,sorg);
  932. consume_all_until(_SEMICOLON);
  933. end
  934. else
  935. with Tvarsym(srsym) do
  936. begin
  937. { check position }
  938. if fieldoffset<aktpos then
  939. message(parser_e_invalid_record_const);
  940. { check in VMT needs to be added for TP mode }
  941. with Tobjectdef(t.def) do
  942. if not(m_fpc in aktmodeswitches) and
  943. (oo_has_vmt in objectoptions) and
  944. (vmt_offset<fieldoffset) then
  945. begin
  946. for i:=1 to vmt_offset-aktpos do
  947. curconstsegment.concat(tai_const.create_8bit(0));
  948. curconstsegment.concat(tai_const_symbol.createname(vmt_mangledname,AT_DATA,0));
  949. { this is more general }
  950. aktpos:=vmt_offset + pointer_size;
  951. end;
  952. { if needed fill }
  953. if fieldoffset>aktpos then
  954. for i:=1 to fieldoffset-aktpos do
  955. curconstSegment.concat(Tai_const.Create_8bit(0));
  956. { new position }
  957. aktpos:=fieldoffset+vartype.def.size;
  958. { read the data }
  959. readtypedconst(vartype,nil,writable);
  960. if token=_SEMICOLON then
  961. consume(_SEMICOLON)
  962. else break;
  963. end;
  964. end;
  965. if not(m_fpc in aktmodeswitches) and
  966. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  967. (tobjectdef(t.def).vmt_offset>=aktpos) then
  968. begin
  969. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  970. curconstsegment.concat(tai_const.create_8bit(0));
  971. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname,AT_DATA,0));
  972. { this is more general }
  973. aktpos:=tobjectdef(t.def).vmt_offset + pointer_size;
  974. end;
  975. for i:=1 to t.def.size-aktpos do
  976. curconstSegment.concat(Tai_const.Create_8bit(0));
  977. consume(_RKLAMMER);
  978. end;
  979. end;
  980. errordef:
  981. begin
  982. { try to consume something useful }
  983. if token=_LKLAMMER then
  984. consume_all_until(_RKLAMMER)
  985. else
  986. consume_all_until(_SEMICOLON);
  987. end;
  988. else Message(parser_e_type_const_not_possible);
  989. end;
  990. block_type:=old_block_type;
  991. end;
  992. {$ifdef fpc}
  993. {$maxfpuregisters default}
  994. {$endif fpc}
  995. end.
  996. {
  997. $Log$
  998. Revision 1.83 2004-04-11 10:44:23 peter
  999. * block_type is bt_const when parsing typed consts
  1000. Revision 1.82 2004/03/18 11:43:57 olle
  1001. * change AT_FUNCTION to AT_DATA where appropriate
  1002. Revision 1.81 2004/03/17 22:27:41 florian
  1003. * fixed handling of doubles in a native arm compiler
  1004. * fixed handling of typed double constants on arm
  1005. Revision 1.80 2004/03/02 00:36:33 olle
  1006. * big transformation of Tai_[const_]Symbol.Create[data]name*
  1007. Revision 1.79 2004/02/26 16:15:23 peter
  1008. * support @@procvar in typed consts
  1009. Revision 1.78 2004/02/07 23:28:34 daniel
  1010. * Take advantage of our new with statement optimization
  1011. Revision 1.77 2003/12/29 12:48:39 jonas
  1012. + support for currency typed constants if currency=int64. Warning: does
  1013. not work properly for extreme values if bestreal <= double
  1014. Revision 1.76 2003/12/08 22:34:24 peter
  1015. * tai_const.create_32bit changed to cardinal
  1016. Revision 1.75 2003/11/22 00:32:35 jonas
  1017. * fixed reversed "got <type 1>, expected <type 1>" error message
  1018. Revision 1.74 2003/11/12 16:05:39 florian
  1019. * assembler readers OOPed
  1020. + typed currency constants
  1021. + typed 128 bit float constants if the CPU supports it
  1022. Revision 1.73 2003/11/08 10:23:35 florian
  1023. * fixed parsing of typed widestring constants with length 1
  1024. Revision 1.72 2003/10/21 18:16:13 peter
  1025. * IncompatibleTypes() added that will include unit names when
  1026. the typenames are the same
  1027. Revision 1.71 2003/09/23 17:56:06 peter
  1028. * locals and paras are allocated in the code generation
  1029. * tvarsym.localloc contains the location of para/local when
  1030. generating code for the current procedure
  1031. Revision 1.70 2003/09/03 15:55:01 peter
  1032. * NEWRA branch merged
  1033. Revision 1.69 2003/05/09 17:47:03 peter
  1034. * self moved to hidden parameter
  1035. * removed hdisposen,hnewn,selfn
  1036. Revision 1.68 2003/04/30 20:53:32 florian
  1037. * error when address of an abstract method is taken
  1038. * fixed some x86-64 problems
  1039. * merged some more x86-64 and i386 code
  1040. Revision 1.67 2003/04/24 22:29:58 florian
  1041. * fixed a lot of PowerPC related stuff
  1042. Revision 1.66 2003/04/06 21:11:23 olle
  1043. * changed newasmsymbol to newasmsymboldata for data symbols
  1044. Revision 1.65 2003/03/17 21:42:32 peter
  1045. * allow nil initialization of dynamic array
  1046. Revision 1.64 2003/01/02 20:45:08 peter
  1047. * fix uninited var
  1048. Revision 1.63 2002/12/26 12:34:54 florian
  1049. * fixed support for type widechar consts
  1050. Revision 1.62 2002/12/07 14:15:33 carl
  1051. + add some explicit typecasts to remove some warnings
  1052. Revision 1.61 2002/11/25 18:43:33 carl
  1053. - removed the invalid if <> checking (Delphi is strange on this)
  1054. + implemented abstract warning on instance creation of class with
  1055. abstract methods.
  1056. * some error message cleanups
  1057. Revision 1.60 2002/11/25 17:43:23 peter
  1058. * splitted defbase in defutil,symutil,defcmp
  1059. * merged isconvertable and is_equal into compare_defs(_ext)
  1060. * made operator search faster by walking the list only once
  1061. Revision 1.59 2002/11/22 22:48:10 carl
  1062. * memory optimization with tconstsym (1.5%)
  1063. Revision 1.58 2002/11/09 15:31:57 carl
  1064. + align ansi/wide string constants
  1065. Revision 1.57 2002/09/06 19:58:31 carl
  1066. * start bugfix 1996
  1067. * 64-bit typed constant now work correctly and fully (bugfix 2001)
  1068. Revision 1.56 2002/09/03 16:26:27 daniel
  1069. * Make Tprocdef.defs protected
  1070. Revision 1.55 2002/08/11 14:32:27 peter
  1071. * renamed current_library to objectlibrary
  1072. Revision 1.54 2002/08/11 13:24:13 peter
  1073. * saving of asmsymbols in ppu supported
  1074. * asmsymbollist global is removed and moved into a new class
  1075. tasmlibrarydata that will hold the info of a .a file which
  1076. corresponds with a single module. Added librarydata to tmodule
  1077. to keep the library info stored for the module. In the future the
  1078. objectfiles will also be stored to the tasmlibrarydata class
  1079. * all getlabel/newasmsymbol and friends are moved to the new class
  1080. Revision 1.53 2002/07/23 12:34:30 daniel
  1081. * Readded old set code. To use it define 'oldset'. Activated by default
  1082. for ppc.
  1083. Revision 1.52 2002/07/22 11:48:04 daniel
  1084. * Sets are now internally sets.
  1085. Revision 1.51 2002/07/20 11:57:56 florian
  1086. * types.pas renamed to defbase.pas because D6 contains a types
  1087. unit so this would conflicts if D6 programms are compiled
  1088. + Willamette/SSE2 instructions to assembler added
  1089. Revision 1.50 2002/07/01 18:46:25 peter
  1090. * internal linker
  1091. * reorganized aasm layer
  1092. Revision 1.49 2002/05/18 13:34:16 peter
  1093. * readded missing revisions
  1094. Revision 1.48 2002/05/16 19:46:44 carl
  1095. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1096. + try to fix temp allocation (still in ifdef)
  1097. + generic constructor calls
  1098. + start of tassembler / tmodulebase class cleanup
  1099. Revision 1.46 2002/05/12 16:53:09 peter
  1100. * moved entry and exitcode to ncgutil and cgobj
  1101. * foreach gets extra argument for passing local data to the
  1102. iterator function
  1103. * -CR checks also class typecasts at runtime by changing them
  1104. into as
  1105. * fixed compiler to cycle with the -CR option
  1106. * fixed stabs with elf writer, finally the global variables can
  1107. be watched
  1108. * removed a lot of routines from cga unit and replaced them by
  1109. calls to cgobj
  1110. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1111. u32bit then the other is typecasted also to u32bit without giving
  1112. a rangecheck warning/error.
  1113. * fixed pascal calling method with reversing also the high tree in
  1114. the parast, detected by tcalcst3 test
  1115. Revision 1.45 2002/04/23 19:16:35 peter
  1116. * add pinline unit that inserts compiler supported functions using
  1117. one or more statements
  1118. * moved finalize and setlength from ninl to pinline
  1119. Revision 1.44 2002/04/20 21:32:24 carl
  1120. + generic FPC_CHECKPOINTER
  1121. + first parameter offset in stack now portable
  1122. * rename some constants
  1123. + move some cpu stuff to other units
  1124. - remove unused constents
  1125. * fix stacksize for some targets
  1126. * fix generic size problems which depend now on EXTEND_SIZE constant
  1127. Revision 1.43 2002/04/15 19:01:53 carl
  1128. + target_info.size_of_pointer -> pointer_Size
  1129. Revision 1.42 2002/04/04 19:06:03 peter
  1130. * removed unused units
  1131. * use tlocation.size in cg.a_*loc*() routines
  1132. Revision 1.41 2002/01/24 18:25:49 peter
  1133. * implicit result variable generation for assembler routines
  1134. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1135. Revision 1.40 2002/01/06 21:47:32 peter
  1136. * removed getprocvar, use only getprocvardef
  1137. }