ptconst.pas 45 KB

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