ptconst.pas 45 KB

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