2
0

cg386con.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for 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 cg386con;
  19. interface
  20. uses
  21. tree;
  22. {$define SMALLSETORD}
  23. procedure secondrealconst(var p : ptree);
  24. procedure secondfixconst(var p : ptree);
  25. procedure secondordconst(var p : ptree);
  26. procedure secondstringconst(var p : ptree);
  27. procedure secondsetconst(var p : ptree);
  28. procedure secondniln(var p : ptree);
  29. implementation
  30. uses
  31. globtype,systems,
  32. cobjects,verbose,globals,
  33. symtable,aasm,types,
  34. hcodegen,temp_gen,pass_2,
  35. i386,cgai386,tgeni386;
  36. {*****************************************************************************
  37. SecondRealConst
  38. *****************************************************************************}
  39. procedure secondrealconst(var p : ptree);
  40. var
  41. hp1 : pai;
  42. lastlabel : plabel;
  43. begin
  44. lastlabel:=nil;
  45. { const already used ? }
  46. if not assigned(p^.lab_real) then
  47. begin
  48. { tries to found an old entry }
  49. hp1:=pai(consts^.first);
  50. while assigned(hp1) do
  51. begin
  52. if hp1^.typ=ait_label then
  53. lastlabel:=pai_label(hp1)^.l
  54. else
  55. begin
  56. if (hp1^.typ=p^.realtyp) and (lastlabel<>nil) then
  57. begin
  58. if ((p^.realtyp=ait_real_64bit) and (pai_double(hp1)^.value=p^.value_real)) or
  59. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.value_real)) or
  60. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.value_real)) then
  61. begin
  62. { found! }
  63. p^.lab_real:=lastlabel;
  64. break;
  65. end;
  66. end;
  67. lastlabel:=nil;
  68. end;
  69. hp1:=pai(hp1^.next);
  70. end;
  71. { :-(, we must generate a new entry }
  72. if not assigned(p^.lab_real) then
  73. begin
  74. getdatalabel(lastlabel);
  75. p^.lab_real:=lastlabel;
  76. if (cs_smartlink in aktmoduleswitches) then
  77. consts^.concat(new(pai_cut,init));
  78. consts^.concat(new(pai_label,init(lastlabel)));
  79. case p^.realtyp of
  80. ait_real_64bit : consts^.concat(new(pai_double,init(p^.value_real)));
  81. ait_real_32bit : consts^.concat(new(pai_single,init(p^.value_real)));
  82. ait_real_extended : consts^.concat(new(pai_extended,init(p^.value_real)));
  83. else
  84. internalerror(10120);
  85. end;
  86. end;
  87. end;
  88. clear_reference(p^.location.reference);
  89. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_real));
  90. p^.location.loc:=LOC_MEM;
  91. end;
  92. {*****************************************************************************
  93. SecondFixConst
  94. *****************************************************************************}
  95. procedure secondfixconst(var p : ptree);
  96. begin
  97. { an fix comma const. behaves as a memory reference }
  98. p^.location.loc:=LOC_MEM;
  99. p^.location.reference.isintvalue:=true;
  100. p^.location.reference.offset:=p^.value_fix;
  101. end;
  102. {*****************************************************************************
  103. SecondOrdConst
  104. *****************************************************************************}
  105. procedure secondordconst(var p : ptree);
  106. begin
  107. { an integer const. behaves as a memory reference }
  108. p^.location.loc:=LOC_MEM;
  109. p^.location.reference.isintvalue:=true;
  110. p^.location.reference.offset:=p^.value;
  111. end;
  112. {*****************************************************************************
  113. SecondStringConst
  114. *****************************************************************************}
  115. procedure secondstringconst(var p : ptree);
  116. var
  117. hp1 : pai;
  118. l1,l2,
  119. lastlabel : plabel;
  120. pc : pchar;
  121. same_string : boolean;
  122. l,
  123. i,mylength : longint;
  124. begin
  125. lastlabel:=nil;
  126. { const already used ? }
  127. if not assigned(p^.lab_str) then
  128. begin
  129. if is_shortstring(p^.resulttype) then
  130. mylength:=p^.length+2
  131. else
  132. mylength:=p^.length+1;
  133. { tries to found an old entry }
  134. hp1:=pai(consts^.first);
  135. while assigned(hp1) do
  136. begin
  137. if hp1^.typ=ait_label then
  138. lastlabel:=pai_label(hp1)^.l
  139. else
  140. begin
  141. { when changing that code, be careful that }
  142. { you don't use typed consts, which are }
  143. { are also written to consts }
  144. { currently, this is no problem, because }
  145. { typed consts have no leading length or }
  146. { they have no trailing zero }
  147. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  148. (pai_string(hp1)^.len=mylength) then
  149. begin
  150. same_string:=true;
  151. for i:=0 to p^.length do
  152. if pai_string(hp1)^.str[i]<>p^.value_str[i] then
  153. begin
  154. same_string:=false;
  155. break;
  156. end;
  157. if same_string then
  158. begin
  159. { found! }
  160. p^.lab_str:=lastlabel;
  161. if (p^.stringtype in [st_ansistring,st_widestring]) then
  162. begin
  163. getdatalabel(l2);
  164. consts^.concat(new(pai_label,init(l2)));
  165. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(p^.lab_str)))));
  166. { return the offset of the real string }
  167. p^.lab_str:=l2;
  168. end;
  169. break;
  170. end;
  171. end;
  172. lastlabel:=nil;
  173. end;
  174. hp1:=pai(hp1^.next);
  175. end;
  176. { :-(, we must generate a new entry }
  177. if not assigned(p^.lab_str) then
  178. begin
  179. getdatalabel(lastlabel);
  180. p^.lab_str:=lastlabel;
  181. if (cs_smartlink in aktmoduleswitches) then
  182. consts^.concat(new(pai_cut,init));
  183. consts^.concat(new(pai_label,init(lastlabel)));
  184. { generate an ansi string ? }
  185. case p^.stringtype of
  186. st_ansistring:
  187. begin
  188. { an empty ansi string is nil! }
  189. if p^.length=0 then
  190. consts^.concat(new(pai_const,init_32bit(0)))
  191. else
  192. begin
  193. getdatalabel(l1);
  194. getdatalabel(l2);
  195. consts^.concat(new(pai_label,init(l2)));
  196. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  197. consts^.concat(new(pai_const,init_32bit(p^.length)));
  198. consts^.concat(new(pai_const,init_32bit(p^.length)));
  199. consts^.concat(new(pai_const,init_32bit(-1)));
  200. consts^.concat(new(pai_label,init(l1)));
  201. getmem(pc,p^.length+2);
  202. move(p^.value_str^,pc^,p^.length);
  203. pc[p^.length]:=#0;
  204. { to overcome this problem we set the length explicitly }
  205. { with the ending null char }
  206. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  207. { return the offset of the real string }
  208. p^.lab_str:=l2;
  209. end;
  210. end;
  211. st_shortstring:
  212. begin
  213. { empty strings }
  214. if p^.length=0 then
  215. consts^.concat(new(pai_const,init_16bit(0)))
  216. else
  217. begin
  218. { truncate strings larger than 255 chars }
  219. if p^.length>255 then
  220. l:=255
  221. else
  222. l:=p^.length;
  223. { also length and terminating zero }
  224. getmem(pc,l+3);
  225. move(p^.value_str^,pc[1],l+1);
  226. pc[0]:=chr(l);
  227. { to overcome this problem we set the length explicitly }
  228. { with the ending null char }
  229. pc[l+1]:=#0;
  230. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  231. end;
  232. end;
  233. end;
  234. end;
  235. end;
  236. clear_reference(p^.location.reference);
  237. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_str));
  238. p^.location.loc:=LOC_MEM;
  239. end;
  240. {*****************************************************************************
  241. SecondSetCons
  242. *****************************************************************************}
  243. procedure secondsetconst(var p : ptree);
  244. var
  245. hp1 : pai;
  246. lastlabel : plabel;
  247. i : longint;
  248. neededtyp : tait;
  249. begin
  250. {$ifdef SMALLSETORD}
  251. { small sets are loaded as constants }
  252. if psetdef(p^.resulttype)^.settype=smallset then
  253. begin
  254. p^.location.loc:=LOC_MEM;
  255. p^.location.reference.isintvalue:=true;
  256. p^.location.reference.offset:=plongint(p^.value_set)^;
  257. exit;
  258. end;
  259. {$endif}
  260. if psetdef(p^.resulttype)^.settype=smallset then
  261. neededtyp:=ait_const_32bit
  262. else
  263. neededtyp:=ait_const_8bit;
  264. lastlabel:=nil;
  265. { const already used ? }
  266. if not assigned(p^.lab_set) then
  267. begin
  268. { tries to found an old entry }
  269. hp1:=pai(consts^.first);
  270. while assigned(hp1) do
  271. begin
  272. if hp1^.typ=ait_label then
  273. lastlabel:=pai_label(hp1)^.l
  274. else
  275. begin
  276. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  277. begin
  278. if (hp1^.typ=ait_const_8bit) then
  279. begin
  280. { compare normal set }
  281. i:=0;
  282. while assigned(hp1) and (i<32) do
  283. begin
  284. if pai_const(hp1)^.value<>p^.value_set^[i] then
  285. break;
  286. inc(i);
  287. hp1:=pai(hp1^.next);
  288. end;
  289. if i=32 then
  290. begin
  291. { found! }
  292. p^.lab_set:=lastlabel;
  293. break;
  294. end;
  295. { leave when the end of consts is reached, so no
  296. hp1^.next is done }
  297. if not assigned(hp1) then
  298. break;
  299. end
  300. else
  301. begin
  302. { compare small set }
  303. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  304. begin
  305. { found! }
  306. p^.lab_set:=lastlabel;
  307. break;
  308. end;
  309. end;
  310. end;
  311. lastlabel:=nil;
  312. end;
  313. hp1:=pai(hp1^.next);
  314. end;
  315. { :-(, we must generate a new entry }
  316. if not assigned(p^.lab_set) then
  317. begin
  318. getdatalabel(lastlabel);
  319. p^.lab_set:=lastlabel;
  320. if (cs_smartlink in aktmoduleswitches) then
  321. consts^.concat(new(pai_cut,init));
  322. consts^.concat(new(pai_label,init(lastlabel)));
  323. if psetdef(p^.resulttype)^.settype=smallset then
  324. begin
  325. move(p^.value_set^,i,sizeof(longint));
  326. consts^.concat(new(pai_const,init_32bit(i)));
  327. end
  328. else
  329. begin
  330. for i:=0 to 31 do
  331. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  332. end;
  333. end;
  334. end;
  335. clear_reference(p^.location.reference);
  336. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_set));
  337. p^.location.loc:=LOC_MEM;
  338. end;
  339. {*****************************************************************************
  340. SecondNilN
  341. *****************************************************************************}
  342. procedure secondniln(var p : ptree);
  343. begin
  344. p^.location.loc:=LOC_MEM;
  345. p^.location.reference.isintvalue:=true;
  346. p^.location.reference.offset:=0;
  347. end;
  348. end.
  349. {
  350. $Log$
  351. Revision 1.27 1999-01-19 14:21:59 peter
  352. * shortstring truncated after 255 chars
  353. Revision 1.26 1998/12/11 00:02:49 peter
  354. + globtype,tokens,version unit splitted from globals
  355. Revision 1.25 1998/12/10 14:39:30 florian
  356. * bug with p(const a : ansistring) fixed
  357. * duplicate constant ansistrings were handled wrong, fixed
  358. Revision 1.24 1998/11/28 15:36:02 michael
  359. Fixed generation of constant ansistrings
  360. Revision 1.23 1998/11/26 14:39:12 peter
  361. * ansistring -> pchar fixed
  362. * ansistring constants fixed
  363. * ansistring constants are now written once
  364. Revision 1.22 1998/11/24 13:40:59 peter
  365. * release smallsetord, so small sets constant are handled like longints
  366. Revision 1.21 1998/11/24 12:52:41 peter
  367. * sets are not written twice anymore
  368. * optimize for emptyset+single element which uses a new routine from
  369. set.inc FPC_SET_CREATE_ELEMENT
  370. Revision 1.20 1998/11/16 12:11:29 peter
  371. * fixed ansistring crash
  372. Revision 1.19 1998/11/05 23:40:45 pierre
  373. * fix for const strings
  374. Revision 1.18 1998/11/05 15:26:38 pierre
  375. * fix for missing zero after string const
  376. Revision 1.17 1998/11/05 12:02:32 peter
  377. * released useansistring
  378. * removed -Sv, its now available in fpc modes
  379. Revision 1.16 1998/11/04 21:07:43 michael
  380. * undid peters change. Constant ansistrings should end on null too cd ..
  381. Revision 1.15 1998/11/04 10:11:36 peter
  382. * ansistring fixes
  383. Revision 1.14 1998/09/17 09:42:13 peter
  384. + pass_2 for cg386
  385. * Message() -> CGMessage() for pass_1/pass_2
  386. Revision 1.13 1998/09/07 18:45:53 peter
  387. * update smartlinking, uses getdatalabel
  388. * renamed ptree.value vars to value_str,value_real,value_set
  389. Revision 1.12 1998/08/28 10:56:57 peter
  390. * removed warnings
  391. Revision 1.11 1998/08/14 18:18:39 peter
  392. + dynamic set contruction
  393. * smallsets are now working (always longint size)
  394. Revision 1.10 1998/08/04 13:22:46 pierre
  395. * weird bug fixed :
  396. a pchar ' ' (simple space or any other letter) was found to
  397. be equal to a string of length zero !!!
  398. thus printing out non sense
  399. found that out while checking Control-C !!
  400. + added column info also in RHIDE format as
  401. it might be usefull later
  402. Revision 1.9 1998/07/20 18:40:10 florian
  403. * handling of ansi string constants should now work
  404. Revision 1.8 1998/07/20 10:23:00 florian
  405. * better ansi string assignement
  406. Revision 1.7 1998/07/18 22:54:25 florian
  407. * some ansi/wide/longstring support fixed:
  408. o parameter passing
  409. o returning as result from functions
  410. Revision 1.6 1998/07/18 17:11:07 florian
  411. + ansi string constants fixed
  412. + switch $H partial implemented
  413. Revision 1.5 1998/06/25 08:48:07 florian
  414. * first version of rtti support
  415. Revision 1.4 1998/06/08 13:13:31 pierre
  416. + temporary variables now in temp_gen.pas unit
  417. because it is processor independent
  418. * mppc68k.bat modified to undefine i386 and support_mmx
  419. (which are defaults for i386)
  420. Revision 1.3 1998/06/05 17:44:11 peter
  421. * splitted cgi386
  422. Revision 1.2 1998/06/05 16:13:31 pierre
  423. * fix for real and string consts inside inlined procs
  424. Revision 1.1 1998/05/23 01:21:02 peter
  425. + aktasmmode, aktoptprocessor, aktoutputformat
  426. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  427. + $LIBNAME to set the library name where the unit will be put in
  428. * splitted cgi386 a bit (codeseg to large for bp7)
  429. * nasm, tasm works again. nasm moved to ag386nsm.pas
  430. }