cg386con.pas 17 KB

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