ptconst.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. {
  2. $Id$
  3. Copyright (c) 1998-2001 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 defines.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,
  34. cutils,globals,widestr,scanner,
  35. symconst,symbase,symdef,aasm,cpuasm,types,verbose,cpubase,
  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. procedure check_range(def:torddef);
  71. begin
  72. if ((tordconstnode(p).value>def.high) or
  73. (tordconstnode(p).value<def.low)) then
  74. begin
  75. if (cs_check_range in aktlocalswitches) then
  76. Message(parser_e_range_check_error)
  77. else
  78. Message(parser_w_range_check_error);
  79. end;
  80. end;
  81. {$R-} {Range check creates problem with init_8bit(-1) !!}
  82. begin
  83. if writable then
  84. curconstsegment:=datasegment
  85. else
  86. curconstsegment:=consts;
  87. case t.def.deftype of
  88. orddef:
  89. begin
  90. p:=comp_expr(true);
  91. case torddef(t.def).typ of
  92. bool8bit :
  93. begin
  94. if is_constboolnode(p) then
  95. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  96. else
  97. Message(cg_e_illegal_expression);
  98. end;
  99. bool16bit :
  100. begin
  101. if is_constboolnode(p) then
  102. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  103. else
  104. Message(cg_e_illegal_expression);
  105. end;
  106. bool32bit :
  107. begin
  108. if is_constboolnode(p) then
  109. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value))
  110. else
  111. Message(cg_e_illegal_expression);
  112. end;
  113. uchar :
  114. begin
  115. if is_constcharnode(p) then
  116. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  117. else
  118. Message(cg_e_illegal_expression);
  119. end;
  120. uwidechar :
  121. begin
  122. if is_constcharnode(p) then
  123. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  124. else
  125. Message(cg_e_illegal_expression);
  126. end;
  127. s8bit,
  128. u8bit :
  129. begin
  130. if is_constintnode(p) then
  131. begin
  132. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  133. check_range(torddef(t.def));
  134. end
  135. else
  136. Message(cg_e_illegal_expression);
  137. end;
  138. u16bit,
  139. s16bit :
  140. begin
  141. if is_constintnode(p) then
  142. begin
  143. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  144. check_range(torddef(t.def));
  145. end
  146. else
  147. Message(cg_e_illegal_expression);
  148. end;
  149. s32bit,
  150. u32bit :
  151. begin
  152. if is_constintnode(p) then
  153. begin
  154. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  155. if torddef(t.def).typ<>u32bit then
  156. check_range(torddef(t.def));
  157. end
  158. else
  159. Message(cg_e_illegal_expression);
  160. end;
  161. s64bit,
  162. u64bit:
  163. begin
  164. if is_constintnode(p) then
  165. begin
  166. if target_info.endian = endian_little then
  167. begin
  168. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  169. if (tordconstnode(p).value<0) and (torddef(t.def).typ = s64bit) then
  170. curconstSegment.concat(Tai_const.Create_32bit(-1))
  171. else
  172. curconstSegment.concat(Tai_const.Create_32bit(0));
  173. end
  174. else
  175. begin
  176. if (tordconstnode(p).value<0) and (torddef(t.def).typ = s64bit) then
  177. curconstSegment.concat(Tai_const.Create_32bit(-1))
  178. else
  179. curconstSegment.concat(Tai_const.Create_32bit(0));
  180. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  181. end;
  182. end
  183. else
  184. Message(cg_e_illegal_expression);
  185. end;
  186. else
  187. internalerror(3799);
  188. end;
  189. p.free;
  190. end;
  191. floatdef:
  192. begin
  193. p:=comp_expr(true);
  194. if is_constrealnode(p) then
  195. value:=trealconstnode(p).value_real
  196. else if is_constintnode(p) then
  197. value:=tordconstnode(p).value
  198. else
  199. Message(cg_e_illegal_expression);
  200. case tfloatdef(t.def).typ of
  201. s32real :
  202. curconstSegment.concat(Tai_real_32bit.Create(value));
  203. s64real :
  204. curconstSegment.concat(Tai_real_64bit.Create(value));
  205. s80real :
  206. curconstSegment.concat(Tai_real_80bit.Create(value));
  207. s64comp :
  208. curconstSegment.concat(Tai_comp_64bit.Create(value));
  209. else
  210. internalerror(18);
  211. end;
  212. p.free;
  213. end;
  214. classrefdef:
  215. begin
  216. p:=comp_expr(true);
  217. case p.nodetype of
  218. loadvmtn:
  219. begin
  220. if not(tobjectdef(tclassrefdef(p.resulttype.def).pointertype.def).is_related(
  221. tobjectdef(tclassrefdef(t.def).pointertype.def))) then
  222. Message(cg_e_illegal_expression);
  223. curconstSegment.concat(Tai_const_symbol.Create(newasmsymbol(tobjectdef(
  224. tclassrefdef(p.resulttype.def).pointertype.def).vmt_mangledname)));
  225. end;
  226. niln:
  227. curconstSegment.concat(Tai_const.Create_32bit(0));
  228. else Message(cg_e_illegal_expression);
  229. end;
  230. p.free;
  231. end;
  232. pointerdef:
  233. begin
  234. p:=comp_expr(true);
  235. if (p.nodetype=typeconvn) and
  236. (ttypeconvnode(p).left.nodetype in [addrn,niln]) and
  237. is_equal(t.def,p.resulttype.def) then
  238. begin
  239. hp:=ttypeconvnode(p).left;
  240. ttypeconvnode(p).left:=nil;
  241. p.free;
  242. p:=hp;
  243. end;
  244. { allows horrible ofs(typeof(TButton)^) code !! }
  245. if (p.nodetype=addrn) and
  246. (taddrnode(p).left.nodetype=derefn) then
  247. begin
  248. hp:=tderefnode(taddrnode(p).left).left;
  249. tderefnode(taddrnode(p).left).left:=nil;
  250. p.free;
  251. p:=hp;
  252. end;
  253. { const pointer ? }
  254. if (p.nodetype = pointerconstn) then
  255. curconstsegment.concat(Tai_const.Create_32bit(
  256. tpointerconstnode(p).value))
  257. { nil pointer ? }
  258. else if p.nodetype=niln then
  259. curconstSegment.concat(Tai_const.Create_32bit(0))
  260. { maybe pchar ? }
  261. else
  262. if is_char(tpointerdef(t.def).pointertype.def) and
  263. (p.nodetype<>addrn) then
  264. begin
  265. getdatalabel(ll);
  266. curconstSegment.concat(Tai_const_symbol.Create(ll));
  267. if p.nodetype=stringconstn then
  268. varalign:=size_2_align(tstringconstnode(p).len)
  269. else
  270. varalign:=0;
  271. varalign:=used_align(varalign,aktalignment.varalignmin,aktalignment.varalignmax);
  272. Consts.concat(Tai_align.Create(varalign));
  273. Consts.concat(Tai_label.Create(ll));
  274. if p.nodetype=stringconstn then
  275. begin
  276. len:=tstringconstnode(p).len;
  277. { For tp7 the maximum lentgh can be 255 }
  278. if (m_tp7 in aktmodeswitches) and
  279. (len>255) then
  280. len:=255;
  281. getmem(ca,len+2);
  282. move(tstringconstnode(p).value_str^,ca^,len+1);
  283. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  284. end
  285. else
  286. if is_constcharnode(p) then
  287. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  288. else
  289. Message(cg_e_illegal_expression);
  290. end
  291. { maybe pwidechar ? }
  292. else
  293. if is_widechar(tpointerdef(t.def).pointertype.def) and
  294. (p.nodetype<>addrn) then
  295. begin
  296. getdatalabel(ll);
  297. curconstSegment.concat(Tai_const_symbol.Create(ll));
  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:=tbinarynode(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 assigned(tprocsym(srsym).defs^.next) then
  377. Message(parser_e_no_overloaded_procvars);
  378. curconstSegment.concat(Tai_const_symbol.Createname_offset(tprocsym(srsym).defs^.def.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. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  428. end
  429. else
  430. begin
  431. { store as longint values in swaped format }
  432. j:=0;
  433. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  434. begin
  435. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  436. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  437. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  438. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  439. Inc(j,4);
  440. end;
  441. end;
  442. end;
  443. end
  444. else
  445. Message(cg_e_illegal_expression);
  446. p.free;
  447. end;
  448. enumdef:
  449. begin
  450. p:=comp_expr(true);
  451. if p.nodetype=ordconstn then
  452. begin
  453. if is_equal(p.resulttype.def,t.def) or
  454. is_subequal(p.resulttype.def,t.def) then
  455. begin
  456. case p.resulttype.def.size of
  457. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  458. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  459. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  460. end;
  461. end
  462. else
  463. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  464. end
  465. else
  466. Message(cg_e_illegal_expression);
  467. p.free;
  468. end;
  469. stringdef:
  470. begin
  471. p:=comp_expr(true);
  472. { load strval and strlength of the constant tree }
  473. if p.nodetype=stringconstn then
  474. begin
  475. { convert to the expected string type so that
  476. for widestrings strval is a pcompilerwidestring }
  477. inserttypeconv(p,t);
  478. strlength:=tstringconstnode(p).len;
  479. strval:=tstringconstnode(p).value_str;
  480. end
  481. else if is_constcharnode(p) then
  482. begin
  483. { strval:=pchar(@tordconstnode(p).value);
  484. THIS FAIL on BIG_ENDIAN MACHINES PM }
  485. c:=chr(tordconstnode(p).value and $ff);
  486. strval:=@c;
  487. strlength:=1
  488. end
  489. else if is_constresourcestringnode(p) then
  490. begin
  491. strval:=pchar(tconstsym(tloadnode(p).symtableentry).valueptr);
  492. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  493. end
  494. else
  495. begin
  496. Message(cg_e_illegal_expression);
  497. strlength:=-1;
  498. end;
  499. if strlength>=0 then
  500. begin
  501. case tstringdef(t.def).string_typ of
  502. st_shortstring:
  503. begin
  504. if strlength>=t.def.size then
  505. begin
  506. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  507. strlength:=t.def.size-1;
  508. end;
  509. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  510. { this can also handle longer strings }
  511. getmem(ca,strlength+1);
  512. move(strval^,ca^,strlength);
  513. ca[strlength]:=#0;
  514. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  515. { fillup with spaces if size is shorter }
  516. if t.def.size>strlength then
  517. begin
  518. getmem(ca,t.def.size-strlength);
  519. { def.size contains also the leading length, so we }
  520. { we have to subtract one }
  521. fillchar(ca[0],t.def.size-strlength-1,' ');
  522. ca[t.def.size-strlength-1]:=#0;
  523. { this can also handle longer strings }
  524. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  525. end;
  526. end;
  527. st_ansistring:
  528. begin
  529. { an empty ansi string is nil! }
  530. if (strlength=0) then
  531. curconstSegment.concat(Tai_const.Create_32bit(0))
  532. else
  533. begin
  534. getdatalabel(ll);
  535. curconstSegment.concat(Tai_const_symbol.Create(ll));
  536. { first write the maximum size }
  537. Consts.concat(Tai_const.Create_32bit(strlength));
  538. { second write the real length }
  539. Consts.concat(Tai_const.Create_32bit(strlength));
  540. { redondent with maxlength but who knows ... (PM) }
  541. { third write use count (set to -1 for safety ) }
  542. Consts.concat(Tai_const.Create_32bit(-1));
  543. Consts.concat(Tai_label.Create(ll));
  544. getmem(ca,strlength+2);
  545. move(strval^,ca^,strlength);
  546. { The terminating #0 to be stored in the .data section (JM) }
  547. ca[strlength]:=#0;
  548. { End of the PChar. The memory has to be allocated because in }
  549. { tai_string.done, there is a freemem(len+1) (JM) }
  550. ca[strlength+1]:=#0;
  551. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  552. end;
  553. end;
  554. st_widestring:
  555. begin
  556. { an empty ansi string is nil! }
  557. if (strlength=0) then
  558. curconstSegment.concat(Tai_const.Create_32bit(0))
  559. else
  560. begin
  561. getdatalabel(ll);
  562. curconstSegment.concat(Tai_const_symbol.Create(ll));
  563. Consts.concat(Tai_const.Create_32bit(strlength));
  564. Consts.concat(Tai_const.Create_32bit(strlength));
  565. Consts.concat(Tai_const.Create_32bit(-1));
  566. Consts.concat(Tai_label.Create(ll));
  567. for i:=0 to strlength-1 do
  568. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  569. { ending #0 }
  570. Consts.concat(Tai_const.Create_16bit(0))
  571. end;
  572. end;
  573. st_longstring:
  574. begin
  575. internalerror(200107081);
  576. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  577. curconstSegment.concat(Tai_const.Create_8bit(0));
  578. getmem(ca,strlength+1);
  579. move(strval^,ca^,strlength);
  580. ca[strlength]:=#0;
  581. generate_pascii(consts,ca,strlength);
  582. curconstSegment.concat(Tai_const.Create_8bit(0));}
  583. end;
  584. end;
  585. end;
  586. p.free;
  587. end;
  588. arraydef:
  589. begin
  590. if token=_LKLAMMER then
  591. begin
  592. consume(_LKLAMMER);
  593. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  594. begin
  595. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  596. consume(_COMMA);
  597. end;
  598. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  599. consume(_RKLAMMER);
  600. end
  601. else
  602. { if array of char then we allow also a string }
  603. if is_char(tarraydef(t.def).elementtype.def) then
  604. begin
  605. p:=comp_expr(true);
  606. if p.nodetype=stringconstn then
  607. begin
  608. len:=tstringconstnode(p).len;
  609. { For tp7 the maximum lentgh can be 255 }
  610. if (m_tp7 in aktmodeswitches) and
  611. (len>255) then
  612. len:=255;
  613. ca:=tstringconstnode(p).value_str;
  614. end
  615. else
  616. if is_constcharnode(p) then
  617. begin
  618. c:=chr(tordconstnode(p).value and $ff);
  619. ca:=@c;
  620. len:=1;
  621. end
  622. else
  623. begin
  624. Message(cg_e_illegal_expression);
  625. len:=0;
  626. end;
  627. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  628. Message(parser_e_string_larger_array);
  629. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  630. begin
  631. if i+1-tarraydef(t.def).lowrange<=len then
  632. begin
  633. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  634. inc(ca);
  635. end
  636. else
  637. {Fill the remaining positions with #0.}
  638. curconstSegment.concat(Tai_const.Create_8bit(0));
  639. end;
  640. p.free;
  641. end
  642. else
  643. begin
  644. { we want the ( }
  645. consume(_LKLAMMER);
  646. end;
  647. end;
  648. procvardef:
  649. begin
  650. { Procvars and pointers are no longer compatible. }
  651. { under tp: =nil or =var under fpc: =nil or =@var }
  652. if token=_NIL then
  653. begin
  654. curconstSegment.concat(Tai_const.Create_32bit(0));
  655. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  656. curconstSegment.concat(Tai_const.Create_32bit(0));
  657. consume(_NIL);
  658. exit;
  659. end;
  660. { you can't assign a value other than NIL to a typed constant }
  661. { which is a "procedure of object", because this also requires }
  662. { address of an object/class instance, which is not known at }
  663. { compile time (JM) }
  664. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  665. Message(parser_e_no_procvarobj_const);
  666. { parse the rest too, so we can continue with error checking }
  667. getprocvardef:=tprocvardef(t.def);
  668. p:=comp_expr(true);
  669. getprocvardef:=nil;
  670. if codegenerror then
  671. begin
  672. p.free;
  673. exit;
  674. end;
  675. { let type conversion check everything needed }
  676. inserttypeconv(p,t);
  677. if codegenerror then
  678. begin
  679. p.free;
  680. exit;
  681. end;
  682. { remove typeconvn, that will normally insert a lea
  683. instruction which is not necessary for us }
  684. if p.nodetype=typeconvn then
  685. begin
  686. hp:=ttypeconvnode(p).left;
  687. ttypeconvnode(p).left:=nil;
  688. p.free;
  689. p:=hp;
  690. end;
  691. { remove addrn which we also don't need here }
  692. if p.nodetype=addrn then
  693. begin
  694. hp:=taddrnode(p).left;
  695. taddrnode(p).left:=nil;
  696. p.free;
  697. p:=hp;
  698. end;
  699. { we now need to have a loadn with a procsym }
  700. if (p.nodetype=loadn) and
  701. (tloadnode(p).symtableentry.typ=procsym) then
  702. begin
  703. curconstSegment.concat(Tai_const_symbol.createname(
  704. tprocsym(tloadnode(p).symtableentry).defs^.def.mangledname));
  705. end
  706. else
  707. Message(cg_e_illegal_expression);
  708. p.free;
  709. end;
  710. { reads a typed constant record }
  711. recorddef:
  712. begin
  713. { KAZ }
  714. if (trecorddef(t.def)=rec_tguid) and
  715. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  716. begin
  717. p:=comp_expr(true);
  718. inserttypeconv(p,cshortstringtype);
  719. if p.nodetype=stringconstn then
  720. begin
  721. s:=strpas(tstringconstnode(p).value_str);
  722. p.free;
  723. if string2guid(s,tmpguid) then
  724. begin
  725. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  726. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  727. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  728. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  729. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  730. end
  731. else
  732. Message(parser_e_improper_guid_syntax);
  733. end
  734. else
  735. begin
  736. p.free;
  737. Message(cg_e_illegal_expression);
  738. exit;
  739. end;
  740. end
  741. else
  742. begin
  743. consume(_LKLAMMER);
  744. aktpos:=0;
  745. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  746. recsym := nil;
  747. while token<>_RKLAMMER do
  748. begin
  749. s:=pattern;
  750. sorg:=orgpattern;
  751. consume(_ID);
  752. consume(_COLON);
  753. error := false;
  754. recsym := tsym(trecorddef(t.def).symtable.search(s));
  755. if not assigned(recsym) then
  756. begin
  757. Message1(sym_e_illegal_field,s);
  758. error := true;
  759. end;
  760. if (not error) and
  761. (not assigned(srsym) or
  762. (s <> srsym.name)) then
  763. { possible variant record (JM) }
  764. begin
  765. { All parts of a variant start at the same offset }
  766. { Also allow jumping from one variant part to another, }
  767. { as long as the offsets match }
  768. if (assigned(srsym) and
  769. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  770. { srsym is not assigned after parsing w2 in the }
  771. { typed const in the next example: }
  772. { type tr = record case byte of }
  773. { 1: (l1,l2: dword); }
  774. { 2: (w1,w2: word); }
  775. { end; }
  776. { const r: tr = (w1:1;w2:1;l2:5); }
  777. (tvarsym(recsym).address = aktpos) then
  778. srsym := recsym
  779. { going backwards isn't allowed in any mode }
  780. else if (tvarsym(recsym).address<aktpos) then
  781. begin
  782. Message(parser_e_invalid_record_const);
  783. error := true;
  784. end
  785. { Delphi allows you to skip fields }
  786. else if (m_delphi in aktmodeswitches) then
  787. begin
  788. Message1(parser_w_skipped_fields_before,sorg);
  789. srsym := recsym;
  790. end
  791. { FPC and TP don't }
  792. else
  793. begin
  794. Message1(parser_e_skipped_fields_before,sorg);
  795. error := true;
  796. end;
  797. end;
  798. if error then
  799. consume_all_until(_SEMICOLON)
  800. else
  801. begin
  802. { if needed fill (alignment) }
  803. if tvarsym(srsym).address>aktpos then
  804. for i:=1 to tvarsym(srsym).address-aktpos do
  805. curconstSegment.concat(Tai_const.Create_8bit(0));
  806. { new position }
  807. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  808. { read the data }
  809. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  810. { keep previous field for checking whether whole }
  811. { record was initialized (JM) }
  812. recsym := srsym;
  813. { goto next field }
  814. srsym := tsym(srsym.indexnext);
  815. if token=_SEMICOLON then
  816. consume(_SEMICOLON)
  817. else break;
  818. end;
  819. end;
  820. { are there any fields left? }
  821. if assigned(srsym) and
  822. { don't complain if there only come other variant parts }
  823. { after the last initialized field }
  824. ((recsym=nil) or
  825. (tvarsym(srsym).address > tvarsym(recsym).address)) then
  826. Message1(parser_h_skipped_fields_after,s);
  827. for i:=1 to t.def.size-aktpos do
  828. curconstSegment.concat(Tai_const.Create_8bit(0));
  829. consume(_RKLAMMER);
  830. end;
  831. end;
  832. { reads a typed object }
  833. objectdef:
  834. begin
  835. if is_class_or_interface(t.def) then
  836. begin
  837. p:=comp_expr(true);
  838. if p.nodetype<>niln then
  839. begin
  840. Message(parser_e_type_const_not_possible);
  841. consume_all_until(_RKLAMMER);
  842. end
  843. else
  844. begin
  845. curconstSegment.concat(Tai_const.Create_32bit(0));
  846. end;
  847. p.free;
  848. end
  849. { for objects we allow it only if it doesn't contain a vmt }
  850. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  851. (m_fpc in aktmodeswitches) then
  852. Message(parser_e_type_const_not_possible)
  853. else
  854. begin
  855. consume(_LKLAMMER);
  856. aktpos:=0;
  857. while token<>_RKLAMMER do
  858. begin
  859. s:=pattern;
  860. sorg:=orgpattern;
  861. consume(_ID);
  862. consume(_COLON);
  863. srsym:=nil;
  864. obj:=tobjectdef(t.def);
  865. symt:=obj.symtable;
  866. while (srsym=nil) and assigned(symt) do
  867. begin
  868. srsym:=tsym(symt.search(s));
  869. if assigned(obj) then
  870. obj:=obj.childof;
  871. if assigned(obj) then
  872. symt:=obj.symtable
  873. else
  874. symt:=nil;
  875. end;
  876. if srsym=nil then
  877. begin
  878. Message1(sym_e_id_not_found,sorg);
  879. consume_all_until(_SEMICOLON);
  880. end
  881. else
  882. begin
  883. { check position }
  884. if tvarsym(srsym).address<aktpos then
  885. Message(parser_e_invalid_record_const);
  886. { check in VMT needs to be added for TP mode }
  887. if not(m_fpc in aktmodeswitches) and
  888. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  889. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  890. begin
  891. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  892. curconstsegment.concat(tai_const.create_8bit(0));
  893. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  894. { this is more general }
  895. aktpos:=tobjectdef(t.def).vmt_offset + pointer_size;
  896. end;
  897. { if needed fill }
  898. if tvarsym(srsym).address>aktpos then
  899. for i:=1 to tvarsym(srsym).address-aktpos do
  900. curconstSegment.concat(Tai_const.Create_8bit(0));
  901. { new position }
  902. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  903. { read the data }
  904. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  905. if token=_SEMICOLON then
  906. consume(_SEMICOLON)
  907. else break;
  908. end;
  909. end;
  910. if not(m_fpc in aktmodeswitches) and
  911. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  912. (tobjectdef(t.def).vmt_offset>=aktpos) then
  913. begin
  914. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  915. curconstsegment.concat(tai_const.create_8bit(0));
  916. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  917. { this is more general }
  918. aktpos:=tobjectdef(t.def).vmt_offset + pointer_size;
  919. end;
  920. for i:=1 to t.def.size-aktpos do
  921. curconstSegment.concat(Tai_const.Create_8bit(0));
  922. consume(_RKLAMMER);
  923. end;
  924. end;
  925. errordef:
  926. begin
  927. { try to consume something useful }
  928. if token=_LKLAMMER then
  929. consume_all_until(_RKLAMMER)
  930. else
  931. consume_all_until(_SEMICOLON);
  932. end;
  933. else Message(parser_e_type_const_not_possible);
  934. end;
  935. end;
  936. {$ifdef fpc}
  937. {$maxfpuregisters default}
  938. {$endif fpc}
  939. end.
  940. {
  941. $Log$
  942. Revision 1.45 2002-04-23 19:16:35 peter
  943. * add pinline unit that inserts compiler supported functions using
  944. one or more statements
  945. * moved finalize and setlength from ninl to pinline
  946. Revision 1.44 2002/04/20 21:32:24 carl
  947. + generic FPC_CHECKPOINTER
  948. + first parameter offset in stack now portable
  949. * rename some constants
  950. + move some cpu stuff to other units
  951. - remove unused constents
  952. * fix stacksize for some targets
  953. * fix generic size problems which depend now on EXTEND_SIZE constant
  954. Revision 1.43 2002/04/15 19:01:53 carl
  955. + target_info.size_of_pointer -> pointer_Size
  956. Revision 1.42 2002/04/04 19:06:03 peter
  957. * removed unused units
  958. * use tlocation.size in cg.a_*loc*() routines
  959. Revision 1.41 2002/01/24 18:25:49 peter
  960. * implicit result variable generation for assembler routines
  961. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  962. Revision 1.40 2002/01/06 21:47:32 peter
  963. * removed getprocvar, use only getprocvardef
  964. Revision 1.39 2001/12/06 17:57:38 florian
  965. + parasym to tparaitem added
  966. Revision 1.38 2001/11/02 22:58:06 peter
  967. * procsym definition rewrite
  968. Revision 1.37 2001/10/29 14:59:48 jonas
  969. * typed constants that are "procedure of object" and which are assigned
  970. nil require 8 bytes of "0" (not 4)
  971. * fixed web bug 1655 (reject the code)
  972. Revision 1.36 2001/10/20 20:30:21 peter
  973. * read only typed const support, switch $J-
  974. Revision 1.35 2001/10/20 17:24:26 peter
  975. * make all sets equal when reading an array of sets. Before it could
  976. mix normal and small sets in the same array!
  977. Revision 1.34 2001/09/19 11:06:03 michael
  978. * realname updated for some hints
  979. * realname used for consts,labels
  980. Revision 1.33 2001/09/17 21:29:12 peter
  981. * merged netbsd, fpu-overflow from fixes branch
  982. Revision 1.32 2001/09/02 21:18:28 peter
  983. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  984. is used for holding target platform pointer values. As those can be
  985. bigger than the source platform.
  986. Revision 1.31 2001/08/26 13:36:47 florian
  987. * some cg reorganisation
  988. * some PPC updates
  989. Revision 1.30 2001/08/01 21:46:41 peter
  990. * support pwidechar in typed const
  991. Revision 1.29 2001/07/30 21:39:26 peter
  992. * declare fpu in rtl for m68k linux
  993. Revision 1.28 2001/07/30 20:59:27 peter
  994. * m68k updates from v10 merged
  995. Revision 1.27 2001/07/08 21:00:15 peter
  996. * various widestring updates, it works now mostly without charset
  997. mapping supported
  998. Revision 1.26 2001/06/29 14:16:57 jonas
  999. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  1000. required to assign the address of a procedure to a procvar, sometimes
  1001. not. Now it is always required) (merged)
  1002. Revision 1.25 2001/06/27 21:37:36 peter
  1003. * v10 merges
  1004. Revision 1.24 2001/06/18 20:36:25 peter
  1005. * -Ur switch (merged)
  1006. * masm fixes (merged)
  1007. * quoted filenames for go32v2 and win32
  1008. Revision 1.23 2001/05/06 17:15:00 jonas
  1009. + detect incomplete typed constant records
  1010. Revision 1.22 2001/04/18 22:01:57 peter
  1011. * registration of targets and assemblers
  1012. Revision 1.21 2001/04/13 01:22:13 peter
  1013. * symtable change to classes
  1014. * range check generation and errors fixed, make cycle DEBUG=1 works
  1015. * memory leaks fixed
  1016. Revision 1.20 2001/04/04 22:43:53 peter
  1017. * remove unnecessary calls to firstpass
  1018. Revision 1.19 2001/04/02 21:20:34 peter
  1019. * resulttype rewrite
  1020. Revision 1.18 2001/03/11 22:58:50 peter
  1021. * getsym redesign, removed the globals srsym,srsymtable
  1022. Revision 1.17 2001/02/04 11:12:16 jonas
  1023. * fixed web bug 1377 & const pointer arithmtic
  1024. Revision 1.16 2001/02/03 00:26:35 peter
  1025. * merged fix for bug 1365
  1026. Revision 1.15 2000/12/25 00:07:28 peter
  1027. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1028. tlinkedlist objects)
  1029. Revision 1.14 2000/12/10 20:24:18 peter
  1030. * allow subtypes for enums
  1031. Revision 1.13 2000/11/29 00:30:38 florian
  1032. * unused units removed from uses clause
  1033. * some changes for widestrings
  1034. Revision 1.12 2000/11/06 15:54:15 florian
  1035. * fixed two bugs to get make cycle work, but it's not enough
  1036. Revision 1.11 2000/11/04 14:25:21 florian
  1037. + merged Attila's changes for interfaces, not tested yet
  1038. Revision 1.10 2000/10/31 22:02:51 peter
  1039. * symtable splitted, no real code changes
  1040. Revision 1.9 2000/10/14 10:14:52 peter
  1041. * moehrendorf oct 2000 rewrite
  1042. Revision 1.8 2000/09/30 13:23:04 peter
  1043. * const array of char and pchar length fixed (merged)
  1044. Revision 1.7 2000/09/24 15:06:25 peter
  1045. * use defines.inc
  1046. Revision 1.6 2000/08/27 16:11:52 peter
  1047. * moved some util functions from globals,cobjects to cutils
  1048. * splitted files into finput,fmodule
  1049. Revision 1.5 2000/08/24 19:13:18 peter
  1050. * allow nil for class typed consts (merged)
  1051. Revision 1.4 2000/08/16 13:06:06 florian
  1052. + support of 64 bit integer constants
  1053. Revision 1.3 2000/08/05 13:25:06 peter
  1054. * packenum 1 fixes (merged)
  1055. Revision 1.2 2000/07/13 11:32:47 michael
  1056. + removed logs
  1057. }