ptconst.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. p : ptree;
  43. i,l,strlength : longint;
  44. ll : plabel;
  45. s : string;
  46. ca : pchar;
  47. aktpos : longint;
  48. pd : pprocdef;
  49. hp1,hp2 : pdefcoll;
  50. value : bestreal;
  51. {problem with fldt !!
  52. anyway .valued is not extended !!
  53. value : double; }
  54. procedure check_range;
  55. begin
  56. if ((p^.value>porddef(def)^.high) or
  57. (p^.value<porddef(def)^.low)) then
  58. Message(parser_e_range_check_error);
  59. end;
  60. {$R-} {Range check creates problem with init_8bit(-1) !!}
  61. begin
  62. case def^.deftype of
  63. orddef:
  64. begin
  65. p:=comp_expr(true);
  66. do_firstpass(p);
  67. case porddef(def)^.typ of
  68. s8bit,
  69. u8bit : begin
  70. if not is_constintnode(p) then
  71. { is't an int expected }
  72. Message(cg_e_illegal_expression)
  73. else
  74. begin
  75. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  76. check_range;
  77. end;
  78. end;
  79. s32bit : begin
  80. if not is_constintnode(p) then
  81. Message(cg_e_illegal_expression)
  82. else
  83. begin
  84. datasegment^.concat(new(pai_const,init_32bit(p^.value)));
  85. check_range;
  86. end;
  87. end;
  88. u32bit : begin
  89. if not is_constintnode(p) then
  90. Message(cg_e_illegal_expression)
  91. else
  92. datasegment^.concat(new(pai_const,init_32bit(p^.value)));
  93. end;
  94. bool8bit : begin
  95. if not is_constboolnode(p) then
  96. Message(cg_e_illegal_expression);
  97. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  98. end;
  99. uchar : begin
  100. if not is_constcharnode(p) then
  101. Message(cg_e_illegal_expression);
  102. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  103. end;
  104. u16bit,
  105. s16bit : begin
  106. if not is_constintnode(p) then
  107. Message(cg_e_illegal_expression);
  108. datasegment^.concat(new(pai_const,init_16bit(p^.value)));
  109. check_range;
  110. end;
  111. end;
  112. disposetree(p);
  113. end;
  114. floatdef:
  115. begin
  116. p:=comp_expr(true);
  117. do_firstpass(p);
  118. if is_constrealnode(p) then
  119. value:=p^.valued
  120. else if is_constintnode(p) then
  121. value:=p^.value
  122. else
  123. Message(cg_e_illegal_expression);
  124. case pfloatdef(def)^.typ of
  125. s64real : datasegment^.concat(new(pai_double,init(value)));
  126. s32real : datasegment^.concat(new(pai_single,init(value)));
  127. s80real : datasegment^.concat(new(pai_extended,init(value)));
  128. s64bit : datasegment^.concat(new(pai_comp,init(value)));
  129. f32bit : datasegment^.concat(new(pai_const,init_32bit(trunc(value*65536))));
  130. else internalerror(18);
  131. end;
  132. disposetree(p);
  133. end;
  134. pointerdef:
  135. begin
  136. p:=comp_expr(true);
  137. do_firstpass(p);
  138. { nil pointer ? }
  139. if p^.treetype=niln then
  140. datasegment^.concat(new(pai_const,init_32bit(0)))
  141. { maybe pchar ? }
  142. else
  143. if (ppointerdef(def)^.definition^.deftype=orddef) and
  144. (porddef(ppointerdef(def)^.definition)^.typ=uchar) then
  145. begin
  146. getlabel(ll);
  147. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(ll)))));
  148. datasegment^.concat(new(pai_label,init(ll)));
  149. { insert string at the begin }
  150. if p^.treetype=stringconstn then
  151. datasegment^.concat(new(pai_string,init(p^.values^+#0)))
  152. else
  153. if is_constcharnode(p) then
  154. datasegment^.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=setconstrn 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. for l:=0 to def^.savesize-1 do
  206. datasegment^.concat(new(pai_const,init_8bit(p^.constset^[l])));
  207. end;
  208. end
  209. else
  210. Message(cg_e_illegal_expression);
  211. disposetree(p);
  212. end;
  213. enumdef:
  214. begin
  215. p:=comp_expr(true);
  216. do_firstpass(p);
  217. if p^.treetype=ordconstn then
  218. begin
  219. if is_equal(p^.resulttype,def) then
  220. datasegment^.concat(new(pai_const,init_32bit(p^.value)))
  221. else
  222. Message(cg_e_illegal_expression);
  223. end
  224. else
  225. Message(cg_e_illegal_expression);
  226. disposetree(p);
  227. end;
  228. stringdef:
  229. begin
  230. p:=comp_expr(true);
  231. do_firstpass(p);
  232. { first take care of prefixes for long and ansi strings }
  233. {$ifdef UseLongString}
  234. if pstringdef(def)^.string_typ=longstring then
  235. begin
  236. { first write the maximum size }
  237. datasegment^.concat(new(pai_const,init_32bit(p^.length)))));
  238. end else
  239. {$endif UseLongString}
  240. {$ifdef UseAnsiString}
  241. if pstringdef(def)^.string_typ=ansistring then
  242. begin
  243. {$ifdef debug}
  244. datasegment^.concat(new(pai_asm_comment,init('Header of ansistring')));
  245. {$endif debug}
  246. { first write the maximum size }
  247. datasegment^.concat(new(pai_const,init_32bit(pstringdef(def)^.len)));
  248. { second write the real length }
  249. datasegment^.concat(new(pai_const,init_32bit(p^.length)));
  250. { redondent with maxlength but who knows ... (PM) }
  251. { third write use count (set to -1 for safety ) }
  252. datasegment^.concat(new(pai_const,init_32bit(-1)));
  253. if assigned(sym) then
  254. sym^.really_insert_in_data;
  255. end;
  256. {$endif UseAnsiString}
  257. { the actual write is independent of the string_typ }
  258. if p^.treetype=stringconstn then
  259. begin
  260. {$ifdef UseAnsiString}
  261. if p^.length>=def^.size then
  262. strlength:=def^.size-1
  263. else
  264. strlength:=p^.length;
  265. { this can also handle longer strings }
  266. generate_pascii(p^.values,strlength);
  267. {$else UseAnsiString}
  268. if length(p^.values^)>=def^.size then
  269. strlength:=def^.size-1
  270. else
  271. strlength:=length(p^.values^);
  272. generate_ascii(char(strlength)+p^.values^);
  273. {$endif UseAnsiString}
  274. end
  275. else if is_constcharnode(p) then
  276. begin
  277. datasegment^.concat(new(pai_string,init(#1+char(byte(p^.value)))));
  278. strlength:=1;
  279. end
  280. else Message(cg_e_illegal_expression);
  281. if def^.size>strlength then
  282. begin
  283. getmem(ca,def^.size-strlength);
  284. fillchar(ca[0],def^.size-strlength-1,' ');
  285. ca[def^.size-strlength-1]:=#0;
  286. {$ifdef UseAnsiString}
  287. { this can also handle longer strings }
  288. generate_pascii(ca,def^.size-strlength);
  289. {$else UseAnsiString}
  290. datasegment^.concat(new(pai_string,init_pchar(ca)));
  291. {$endif UseAnsiString}
  292. disposetree(p);
  293. end;
  294. end;
  295. arraydef:
  296. begin
  297. if token=LKLAMMER then
  298. begin
  299. consume(LKLAMMER);
  300. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  301. begin
  302. readtypedconst(parraydef(def)^.definition,nil);
  303. consume(COMMA);
  304. end;
  305. readtypedconst(parraydef(def)^.definition,nil);
  306. consume(RKLAMMER);
  307. end
  308. else
  309. begin
  310. p:=comp_expr(true);
  311. do_firstpass(p);
  312. if p^.treetype=stringconstn then
  313. s:=p^.values^
  314. else if is_constcharnode(p) then
  315. s:=char(byte(p^.value))
  316. else Message(cg_e_illegal_expression);
  317. l:=length(s);
  318. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  319. begin
  320. if i+1-Parraydef(def)^.lowrange<=l then
  321. begin
  322. datasegment^.concat(new(pai_const,init_8bit(byte(s[1]))));
  323. delete(s,1,1);
  324. end
  325. else
  326. {Fill the remaining positions with #0.}
  327. datasegment^.concat(new(pai_const,init_8bit(0)));
  328. end;
  329. if length(s)>0 then
  330. Message(parser_e_string_too_long);
  331. end;
  332. end;
  333. procvardef:
  334. begin
  335. { Procvars and pointers are no longer compatible. }
  336. { under tp: =nil or =var under fpc: =nil or =@var }
  337. if token=_NIL then
  338. begin
  339. datasegment^.concat(new(pai_const,init_32bit(0)));
  340. consume(_NIL);
  341. exit;
  342. end
  343. else
  344. if not(cs_tp_compatible in aktswitches) then
  345. if token=KLAMMERAFFE then
  346. consume(KLAMMERAFFE);
  347. getsym(pattern,true);
  348. consume(ID);
  349. if srsym^.typ=unitsym then
  350. begin
  351. consume(POINT);
  352. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  353. consume(ID);
  354. end;
  355. if srsym^.typ<>procsym then
  356. Message(cg_e_illegal_expression)
  357. else
  358. begin
  359. pd:=pprocsym(srsym)^.definition;
  360. if assigned(pd^.nextoverloaded) then
  361. Message(parser_e_no_overloaded_procvars);
  362. if not((pprocvardef(def)^.options=pd^.options)) or
  363. not(is_equal(pprocvardef(def)^.retdef,pd^.retdef)) then
  364. Message(sym_e_type_mismatch)
  365. else
  366. begin
  367. hp1:=pprocvardef(def)^.para1;
  368. hp2:=pd^.para1;
  369. while assigned(hp1) and assigned(hp2) do
  370. begin
  371. if not(is_equal(hp1^.data,hp2^.data)) or
  372. not(hp1^.paratyp=hp2^.paratyp) then
  373. begin
  374. Message(sym_e_type_mismatch);
  375. break;
  376. end;
  377. hp1:=hp1^.next;
  378. hp2:=hp2^.next;
  379. end;
  380. if not((hp1=nil) and (hp2=nil)) then
  381. Message(sym_e_type_mismatch);
  382. end;
  383. datasegment^.concat(new(pai_const,init_symbol(strpnew(pd^.mangledname))));
  384. if pd^.owner^.symtabletype=unitsymtable then
  385. concat_external(pd^.mangledname,EXT_NEAR);
  386. end;
  387. end;
  388. { reads a typed constant record }
  389. recorddef:
  390. begin
  391. consume(LKLAMMER);
  392. aktpos:=0;
  393. while token<>RKLAMMER do
  394. begin
  395. s:=pattern;
  396. consume(ID);
  397. consume(COLON);
  398. srsym:=precdef(def)^.symtable^.search(s);
  399. if srsym=nil then
  400. begin
  401. Message1(sym_e_id_not_found,s);
  402. consume_all_until(SEMICOLON);
  403. end
  404. else
  405. begin
  406. { check position }
  407. if pvarsym(srsym)^.address<aktpos then
  408. Message(parser_e_invalid_record_const);
  409. { if needed fill }
  410. if pvarsym(srsym)^.address>aktpos then
  411. for i:=1 to pvarsym(srsym)^.address-aktpos do
  412. datasegment^.concat(new(pai_const,init_8bit(0)));
  413. { new position }
  414. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.definition^.size;
  415. { read the data }
  416. readtypedconst(pvarsym(srsym)^.definition,nil);
  417. if token=SEMICOLON then
  418. consume(SEMICOLON)
  419. else break;
  420. end;
  421. end;
  422. for i:=1 to def^.size-aktpos do
  423. datasegment^.concat(new(pai_const,init_8bit(0)));
  424. consume(RKLAMMER);
  425. end;
  426. else Message(parser_e_type_const_not_possible);
  427. end;
  428. end;
  429. end.
  430. {
  431. $Log$
  432. Revision 1.6 1998-06-08 22:59:52 peter
  433. * smartlinking works for win32
  434. * some defines to exclude some compiler parts
  435. Revision 1.5 1998/06/03 22:49:01 peter
  436. + wordbool,longbool
  437. * rename bis,von -> high,low
  438. * moved some systemunit loading/creating to psystem.pas
  439. Revision 1.4 1998/05/05 12:05:42 florian
  440. * problems with properties fixed
  441. * crash fixed: i:=l when i and l are undefined, was a problem with
  442. implementation of private/protected
  443. Revision 1.3 1998/04/29 10:34:00 pierre
  444. + added some code for ansistring (not complete nor working yet)
  445. * corrected operator overloading
  446. * corrected nasm output
  447. + started inline procedures
  448. + added starstarn : use ** for exponentiation (^ gave problems)
  449. + started UseTokenInfo cond to get accurate positions
  450. }