ptconst.pas 48 KB

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