cg386con.pas 18 KB

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