ptconst.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. interface
  20. uses symtable;
  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(def : pdef;sym : ptypedconstsym);
  25. implementation
  26. uses
  27. cobjects,globals,scanner,aasm,tree,pass_1,
  28. hcodegen,types,verbose
  29. { parser specific stuff }
  30. ,pbase,pexpr
  31. { processor specific stuff }
  32. {$ifdef i386}
  33. ,i386
  34. {$endif}
  35. {$ifdef m68k}
  36. ,m68k
  37. {$endif}
  38. ;
  39. { this procedure reads typed constants }
  40. procedure readtypedconst(def : pdef;sym : ptypedconstsym);
  41. var
  42. {$ifdef m68k}
  43. j : longint;
  44. {$endif m68k}
  45. p : ptree;
  46. i,l,
  47. strlength : longint;
  48. ll : plabel;
  49. s : string;
  50. ca : pchar;
  51. aktpos : longint;
  52. pd : pprocdef;
  53. hp1,hp2 : pdefcoll;
  54. value : bestreal;
  55. procedure check_range;
  56. begin
  57. if ((p^.value>porddef(def)^.high) or
  58. (p^.value<porddef(def)^.low)) then
  59. Message(parser_e_range_check_error);
  60. end;
  61. {$R-} {Range check creates problem with init_8bit(-1) !!}
  62. begin
  63. case def^.deftype of
  64. orddef:
  65. begin
  66. p:=comp_expr(true);
  67. do_firstpass(p);
  68. case porddef(def)^.typ of
  69. s8bit,
  70. u8bit : begin
  71. if not is_constintnode(p) then
  72. { is't an int expected }
  73. Message(cg_e_illegal_expression)
  74. else
  75. begin
  76. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  77. check_range;
  78. end;
  79. end;
  80. s32bit : begin
  81. if not is_constintnode(p) then
  82. Message(cg_e_illegal_expression)
  83. else
  84. begin
  85. datasegment^.concat(new(pai_const,init_32bit(p^.value)));
  86. check_range;
  87. end;
  88. end;
  89. u32bit : begin
  90. if not is_constintnode(p) then
  91. Message(cg_e_illegal_expression)
  92. else
  93. datasegment^.concat(new(pai_const,init_32bit(p^.value)));
  94. end;
  95. bool8bit : begin
  96. if not is_constboolnode(p) then
  97. Message(cg_e_illegal_expression);
  98. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  99. end;
  100. uchar : begin
  101. if not is_constcharnode(p) then
  102. Message(cg_e_illegal_expression);
  103. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  104. end;
  105. u16bit,
  106. s16bit : begin
  107. if not is_constintnode(p) then
  108. Message(cg_e_illegal_expression);
  109. datasegment^.concat(new(pai_const,init_16bit(p^.value)));
  110. check_range;
  111. end;
  112. end;
  113. disposetree(p);
  114. end;
  115. floatdef:
  116. begin
  117. p:=comp_expr(true);
  118. do_firstpass(p);
  119. if is_constrealnode(p) then
  120. value:=p^.value_real
  121. else if is_constintnode(p) then
  122. value:=p^.value
  123. else
  124. Message(cg_e_illegal_expression);
  125. case pfloatdef(def)^.typ of
  126. s64real : datasegment^.concat(new(pai_double,init(value)));
  127. s32real : datasegment^.concat(new(pai_single,init(value)));
  128. s80real : datasegment^.concat(new(pai_extended,init(value)));
  129. s64bit : datasegment^.concat(new(pai_comp,init(value)));
  130. f32bit : datasegment^.concat(new(pai_const,init_32bit(trunc(value*65536))));
  131. else internalerror(18);
  132. end;
  133. disposetree(p);
  134. end;
  135. pointerdef:
  136. begin
  137. p:=comp_expr(true);
  138. do_firstpass(p);
  139. { nil pointer ? }
  140. if p^.treetype=niln then
  141. datasegment^.concat(new(pai_const,init_32bit(0)))
  142. { maybe pchar ? }
  143. else
  144. if (ppointerdef(def)^.definition^.deftype=orddef) and
  145. (porddef(ppointerdef(def)^.definition)^.typ=uchar) then
  146. begin
  147. getdatalabel(ll);
  148. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(ll)))));
  149. consts^.concat(new(pai_label,init(ll)));
  150. if p^.treetype=stringconstn then
  151. consts^.concat(new(pai_string,init(p^.value_str^+#0)))
  152. else
  153. if is_constcharnode(p) then
  154. consts^.concat(new(pai_string,init(char(byte(p^.value))+#0)))
  155. else
  156. Message(cg_e_illegal_expression);
  157. { insert label }
  158. end
  159. else
  160. if p^.treetype=addrn then
  161. begin
  162. if (is_equal(ppointerdef(p^.resulttype)^.definition,ppointerdef(def)^.definition) or
  163. (is_equal(ppointerdef(p^.resulttype)^.definition,voiddef)) or
  164. (is_equal(ppointerdef(def)^.definition,voiddef))) and
  165. (p^.left^.treetype = loadn) then
  166. begin
  167. datasegment^.concat(new(pai_const,init_symbol(
  168. strpnew(p^.left^.symtableentry^.mangledname))));
  169. maybe_concat_external(p^.left^.symtableentry^.owner,
  170. p^.left^.symtableentry^.mangledname);
  171. end
  172. else
  173. Message(cg_e_illegal_expression);
  174. end
  175. else
  176. { allow typeof(Object type)}
  177. if (p^.treetype=inlinen) and
  178. (p^.inlinenumber=in_typeof_x) then
  179. begin
  180. if (p^.left^.treetype=typen) then
  181. begin
  182. datasegment^.concat(new(pai_const,init_symbol(
  183. strpnew(pobjectdef(p^.left^.resulttype)^.vmt_mangledname))));
  184. if pobjectdef(p^.left^.resulttype)^.owner^.symtabletype=unitsymtable then
  185. concat_external(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,EXT_NEAR);
  186. end
  187. else
  188. Message(cg_e_illegal_expression);
  189. end
  190. else
  191. Message(cg_e_illegal_expression);
  192. disposetree(p);
  193. end;
  194. setdef:
  195. begin
  196. p:=comp_expr(true);
  197. do_firstpass(p);
  198. if p^.treetype=setconstn then
  199. begin
  200. { we only allow const sets }
  201. if assigned(p^.left) then
  202. Message(cg_e_illegal_expression)
  203. else
  204. begin
  205. {$ifdef i386}
  206. for l:=0 to def^.savesize-1 do
  207. datasegment^.concat(new(pai_const,init_8bit(p^.value_set^[l])));
  208. {$endif}
  209. {$ifdef m68k}
  210. j:=0;
  211. for l:=0 to ((def^.savesize-1) div 4) do
  212. { HORRIBLE HACK because of endian }
  213. { now use intel endian for constant sets }
  214. begin
  215. datasegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+3])));
  216. datasegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+2])));
  217. datasegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+1])));
  218. datasegment^.concat(new(pai_const,init_8bit(p^.value_set^[j])));
  219. Inc(j,4);
  220. end;
  221. {$endif}
  222. end;
  223. end
  224. else
  225. Message(cg_e_illegal_expression);
  226. disposetree(p);
  227. end;
  228. enumdef:
  229. begin
  230. p:=comp_expr(true);
  231. do_firstpass(p);
  232. if p^.treetype=ordconstn then
  233. begin
  234. if is_equal(p^.resulttype,def) then
  235. datasegment^.concat(new(pai_const,init_32bit(p^.value)))
  236. else
  237. Message(cg_e_illegal_expression);
  238. end
  239. else
  240. Message(cg_e_illegal_expression);
  241. disposetree(p);
  242. end;
  243. stringdef:
  244. begin
  245. p:=comp_expr(true);
  246. do_firstpass(p);
  247. { first take care of prefixes for long and ansi strings }
  248. case pstringdef(def)^.string_typ of
  249. st_shortstring:
  250. begin
  251. if p^.treetype=stringconstn then
  252. begin
  253. {$ifdef UseAnsiString}
  254. if p^.length>=def^.size then
  255. strlength:=def^.size-1
  256. else
  257. strlength:=p^.length;
  258. datasegment^.concat(new(pai_const,init_8bit(strlength)));
  259. { this can also handle longer strings }
  260. generate_pascii(datasegment,p^.value_str,strlength);
  261. {$else UseAnsiString}
  262. if length(p^.value_str^)>=def^.size then
  263. begin
  264. strlength:=def^.size-1;
  265. generate_ascii(datasegment,char(strlength)+copy(p^.value_str^,1,strlength));
  266. end
  267. else
  268. begin
  269. strlength:=length(p^.value_str^);
  270. generate_ascii(datasegment,char(strlength)+p^.value_str^);
  271. end;
  272. {$endif UseAnsiString}
  273. end
  274. else if is_constcharnode(p) then
  275. begin
  276. datasegment^.concat(new(pai_string,init(#1+char(byte(p^.value)))));
  277. strlength:=1;
  278. end
  279. else Message(cg_e_illegal_expression);
  280. if def^.size>strlength then
  281. begin
  282. getmem(ca,def^.size-strlength);
  283. fillchar(ca[0],def^.size-strlength-1,' ');
  284. ca[def^.size-strlength-1]:=#0;
  285. {$ifdef UseAnsiString}
  286. { this can also handle longer strings }
  287. { def^.size contains also the leading length, so we }
  288. { we have to subtract one }
  289. generate_pascii(datasegment,ca,def^.size-strlength-1);
  290. {$else UseAnsiString}
  291. datasegment^.concat(new(pai_string,init_pchar(ca)));
  292. {$endif UseAnsiString}
  293. end;
  294. end;
  295. {$ifdef UseLongString}
  296. st_longstring:
  297. begin
  298. { first write the maximum size }
  299. datasegment^.concat(new(pai_const,init_32bit(p^.length)))));
  300. { fill byte }
  301. datasegment^.concat(new(pai_const,init_8bit(0)));
  302. if p^.treetype=stringconstn then
  303. begin
  304. { this can also handle longer strings }
  305. generate_pascii(consts,p^.value_str,p^.length);
  306. end
  307. else if is_constcharnode(p) then
  308. begin
  309. consts^.concat(new(pai_const,init_8bit(p^.value)));
  310. strlength:=1;
  311. end
  312. else Message(cg_e_illegal_expression);
  313. datasegment^.concat(new(pai_const,init_8bit(0)));
  314. end;
  315. {$endif UseLongString}
  316. {$ifdef UseAnsiString}
  317. st_ansistring:
  318. begin
  319. { an empty ansi string is nil! }
  320. if (p^.treetype=stringconstn) and (p^.length=0) then
  321. datasegment^.concat(new(pai_const,init_32bit(0)))
  322. else
  323. begin
  324. getdatalabel(ll);
  325. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(ll)))));
  326. { first write the maximum size }
  327. consts^.concat(new(pai_const,init_32bit(p^.length)));
  328. { second write the real length }
  329. consts^.concat(new(pai_const,init_32bit(p^.length)));
  330. { redondent with maxlength but who knows ... (PM) }
  331. { third write use count (set to -1 for safety ) }
  332. consts^.concat(new(pai_const,init_32bit(-1)));
  333. { not longer necessary, because it insert_indata
  334. if assigned(sym) then
  335. sym^.really_insert_in_data;
  336. }
  337. consts^.concat(new(pai_label,init(ll)));
  338. if p^.treetype=stringconstn then
  339. begin
  340. { this can also handle longer strings }
  341. generate_pascii(consts,p^.value_str,p^.length);
  342. end
  343. else if is_constcharnode(p) then
  344. begin
  345. consts^.concat(new(pai_const,init_8bit(p^.value)));
  346. strlength:=1;
  347. end
  348. else Message(cg_e_illegal_expression);
  349. consts^.concat(new(pai_const,init_8bit(0)));
  350. end;
  351. end;
  352. {$endif UseAnsiString}
  353. end;
  354. disposetree(p);
  355. end;
  356. arraydef:
  357. begin
  358. if token=LKLAMMER then
  359. begin
  360. consume(LKLAMMER);
  361. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  362. begin
  363. readtypedconst(parraydef(def)^.definition,nil);
  364. consume(COMMA);
  365. end;
  366. readtypedconst(parraydef(def)^.definition,nil);
  367. consume(RKLAMMER);
  368. end
  369. else
  370. begin
  371. p:=comp_expr(true);
  372. do_firstpass(p);
  373. if p^.treetype=stringconstn then
  374. s:=p^.value_str^
  375. else if is_constcharnode(p) then
  376. s:=char(byte(p^.value))
  377. else Message(cg_e_illegal_expression);
  378. disposetree(p);
  379. l:=length(s);
  380. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  381. begin
  382. if i+1-Parraydef(def)^.lowrange<=l then
  383. begin
  384. datasegment^.concat(new(pai_const,init_8bit(byte(s[1]))));
  385. delete(s,1,1);
  386. end
  387. else
  388. {Fill the remaining positions with #0.}
  389. datasegment^.concat(new(pai_const,init_8bit(0)));
  390. end;
  391. if length(s)>0 then
  392. Message(parser_e_string_too_long);
  393. end;
  394. end;
  395. procvardef:
  396. begin
  397. { Procvars and pointers are no longer compatible. }
  398. { under tp: =nil or =var under fpc: =nil or =@var }
  399. if token=_NIL then
  400. begin
  401. datasegment^.concat(new(pai_const,init_32bit(0)));
  402. consume(_NIL);
  403. exit;
  404. end
  405. else
  406. if not(m_tp_procvar in aktmodeswitches) then
  407. if token=KLAMMERAFFE then
  408. consume(KLAMMERAFFE);
  409. getsym(pattern,true);
  410. consume(ID);
  411. if srsym^.typ=unitsym then
  412. begin
  413. consume(POINT);
  414. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  415. consume(ID);
  416. end;
  417. if srsym^.typ<>procsym then
  418. Message(cg_e_illegal_expression)
  419. else
  420. begin
  421. pd:=pprocsym(srsym)^.definition;
  422. if assigned(pd^.nextoverloaded) then
  423. Message(parser_e_no_overloaded_procvars);
  424. if not((pprocvardef(def)^.options=pd^.options)) or
  425. not(is_equal(pprocvardef(def)^.retdef,pd^.retdef)) then
  426. Message(type_e_mismatch)
  427. else
  428. begin
  429. hp1:=pprocvardef(def)^.para1;
  430. hp2:=pd^.para1;
  431. while assigned(hp1) and assigned(hp2) do
  432. begin
  433. if not(is_equal(hp1^.data,hp2^.data)) or
  434. not(hp1^.paratyp=hp2^.paratyp) then
  435. begin
  436. Message(type_e_mismatch);
  437. break;
  438. end;
  439. hp1:=hp1^.next;
  440. hp2:=hp2^.next;
  441. end;
  442. if not((hp1=nil) and (hp2=nil)) then
  443. Message(type_e_mismatch);
  444. end;
  445. datasegment^.concat(new(pai_const,init_symbol(strpnew(pd^.mangledname))));
  446. if pd^.owner^.symtabletype=unitsymtable then
  447. concat_external(pd^.mangledname,EXT_NEAR);
  448. end;
  449. end;
  450. { reads a typed constant record }
  451. recorddef:
  452. begin
  453. consume(LKLAMMER);
  454. aktpos:=0;
  455. while token<>RKLAMMER do
  456. begin
  457. s:=pattern;
  458. consume(ID);
  459. consume(COLON);
  460. srsym:=precdef(def)^.symtable^.search(s);
  461. if srsym=nil then
  462. begin
  463. Message1(sym_e_id_not_found,s);
  464. consume_all_until(SEMICOLON);
  465. end
  466. else
  467. begin
  468. { check position }
  469. if pvarsym(srsym)^.address<aktpos then
  470. Message(parser_e_invalid_record_const);
  471. { if needed fill }
  472. if pvarsym(srsym)^.address>aktpos then
  473. for i:=1 to pvarsym(srsym)^.address-aktpos do
  474. datasegment^.concat(new(pai_const,init_8bit(0)));
  475. { new position }
  476. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.definition^.size;
  477. { read the data }
  478. readtypedconst(pvarsym(srsym)^.definition,nil);
  479. if token=SEMICOLON then
  480. consume(SEMICOLON)
  481. else break;
  482. end;
  483. end;
  484. for i:=1 to def^.size-aktpos do
  485. datasegment^.concat(new(pai_const,init_8bit(0)));
  486. consume(RKLAMMER);
  487. end;
  488. else Message(parser_e_type_const_not_possible);
  489. end;
  490. end;
  491. end.
  492. {
  493. $Log$
  494. Revision 1.17 1998-10-09 08:56:29 pierre
  495. * several memory leaks fixed
  496. Revision 1.16 1998/09/24 23:49:18 peter
  497. + aktmodeswitches
  498. Revision 1.15 1998/09/07 18:46:11 peter
  499. * update smartlinking, uses getdatalabel
  500. * renamed ptree.value vars to value_str,value_real,value_set
  501. Revision 1.14 1998/09/04 08:42:07 peter
  502. * updated some error messages
  503. Revision 1.13 1998/09/01 09:05:36 peter
  504. * fixed string[4]='.library'
  505. Revision 1.12 1998/08/31 12:26:32 peter
  506. * m68k and palmos updates from surebugfixes
  507. Revision 1.11 1998/08/10 14:50:20 peter
  508. + localswitches, moduleswitches, globalswitches splitting
  509. Revision 1.10 1998/07/21 11:16:25 florian
  510. * bug0147 fixed
  511. Revision 1.9 1998/07/20 22:17:16 florian
  512. * hex constants in numeric char (#$54#$43 ...) are now allowed
  513. * there was a bug in record_var_dec which prevents the used
  514. of nested variant records (for example drivers.tevent of tv)
  515. Revision 1.8 1998/07/20 18:40:15 florian
  516. * handling of ansi string constants should now work
  517. Revision 1.7 1998/07/18 22:54:29 florian
  518. * some ansi/wide/longstring support fixed:
  519. o parameter passing
  520. o returning as result from functions
  521. Revision 1.6 1998/06/08 22:59:52 peter
  522. * smartlinking works for win32
  523. * some defines to exclude some compiler parts
  524. Revision 1.5 1998/06/03 22:49:01 peter
  525. + wordbool,longbool
  526. * rename bis,von -> high,low
  527. * moved some systemunit loading/creating to psystem.pas
  528. Revision 1.4 1998/05/05 12:05:42 florian
  529. * problems with properties fixed
  530. * crash fixed: i:=l when i and l are undefined, was a problem with
  531. implementation of private/protected
  532. Revision 1.3 1998/04/29 10:34:00 pierre
  533. + added some code for ansistring (not complete nor working yet)
  534. * corrected operator overloading
  535. * corrected nasm output
  536. + started inline procedures
  537. + added starstarn : use ** for exponentiation (^ gave problems)
  538. + started UseTokenInfo cond to get accurate positions
  539. }