cg386con.pas 18 KB

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