cg386con.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. {$ifdef UseAnsiString}
  118. l1,
  119. {$endif}
  120. lastlabel : plabel;
  121. pc : pchar;
  122. same_string : boolean;
  123. i : longint;
  124. begin
  125. lastlabel:=nil;
  126. { const already used ? }
  127. if not assigned(p^.lab_str) then
  128. begin
  129. { tries to found an old entry }
  130. hp1:=pai(consts^.first);
  131. while assigned(hp1) do
  132. begin
  133. if hp1^.typ=ait_label then
  134. lastlabel:=pai_label(hp1)^.l
  135. else
  136. begin
  137. { when changing that code, be careful that }
  138. { you don't use typed consts, which are }
  139. { are also written to consts }
  140. { currently, this is no problem, because }
  141. { typed consts have no leading length or }
  142. { they have no trailing zero }
  143. {$ifdef UseAnsiString}
  144. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  145. (pai_string(hp1)^.len=p^.length+2) then
  146. {$else UseAnsiString}
  147. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  148. (pai_string(hp1)^.len=length(p^.value_str^)+2) then
  149. {$endif UseAnsiString}
  150. begin
  151. same_string:=true;
  152. {$ifndef UseAnsiString}
  153. for i:=0 to length(p^.value_str^) do
  154. if pai_string(hp1)^.str[i]<>p^.value_str^[i] then
  155. {$else}
  156. for i:=0 to p^.length do
  157. if pai_string(hp1)^.str[i]<>p^.value_str[i] then
  158. {$endif}
  159. begin
  160. same_string:=false;
  161. break;
  162. end;
  163. if same_string then
  164. begin
  165. { found! }
  166. p^.lab_str:=lastlabel;
  167. break;
  168. end;
  169. end;
  170. lastlabel:=nil;
  171. end;
  172. hp1:=pai(hp1^.next);
  173. end;
  174. { :-(, we must generate a new entry }
  175. if not assigned(p^.lab_str) then
  176. begin
  177. getdatalabel(lastlabel);
  178. p^.lab_str:=lastlabel;
  179. if (cs_smartlink in aktmoduleswitches) then
  180. consts^.concat(new(pai_cut,init));
  181. consts^.concat(new(pai_label,init(lastlabel)));
  182. {$ifndef UseAnsiString}
  183. getmem(pc,length(p^.value_str^)+3);
  184. move(p^.value_str^,pc^,length(p^.value_str^)+1);
  185. pc[length(p^.value_str^)+1]:=#0;
  186. { we still will have a problem if there is a #0 inside the pchar }
  187. consts^.concat(new(pai_string,init_length_pchar(pc,length(p^.value_str^)+2)));
  188. {$else UseAnsiString}
  189. { generate an ansi string ? }
  190. case p^.stringtype of
  191. st_ansistring:
  192. begin
  193. { an empty ansi string is nil! }
  194. if p^.length=0 then
  195. consts^.concat(new(pai_const,init_32bit(0)))
  196. else
  197. begin
  198. getlabel(l1);
  199. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  200. consts^.concat(new(pai_const,init_32bit(p^.length)));
  201. consts^.concat(new(pai_const,init_32bit(p^.length)));
  202. consts^.concat(new(pai_const,init_32bit(-1)));
  203. consts^.concat(new(pai_label,init(l1)));
  204. getmem(pc,p^.length+1);
  205. move(p^.value_str^,pc^,p^.length+1);
  206. { to overcome this problem we set the length explicitly }
  207. { with the ending null char }
  208. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  209. end;
  210. end;
  211. st_shortstring:
  212. begin
  213. getmem(pc,p^.length+3);
  214. move(p^.value_str^,pc[1],p^.length+1);
  215. pc[0]:=chr(p^.length);
  216. { to overcome this problem we set the length explicitly }
  217. { with the ending null char }
  218. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  219. end;
  220. end;
  221. {$endif UseAnsiString}
  222. end;
  223. end;
  224. clear_reference(p^.location.reference);
  225. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_str));
  226. p^.location.loc:=LOC_MEM;
  227. end;
  228. {*****************************************************************************
  229. SecondSetCons
  230. *****************************************************************************}
  231. procedure secondsetconst(var p : ptree);
  232. var
  233. lastlabel : plabel;
  234. i : longint;
  235. begin
  236. {$ifdef SMALLSETORD}
  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:=p^.value_set^[0];
  242. end
  243. else
  244. begin
  245. getdatalabel(lastlabel);
  246. p^.lab_set:=lastlabel;
  247. if (cs_smartlink in aktmoduleswitches) then
  248. consts^.concat(new(pai_cut,init));
  249. consts^.concat(new(pai_label,init(duplabel(lastlabel))));
  250. for i:=0 to 31 do
  251. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  252. clear_reference(p^.location.reference);
  253. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_set));
  254. p^.location.loc:=LOC_MEM;
  255. end;
  256. {$else}
  257. getdatalabel(lastlabel);
  258. p^.lab_set:=lastlabel;
  259. if (cs_smartlink in aktmoduleswitches) then
  260. consts^.concat(new(pai_cut,init));
  261. consts^.concat(new(pai_label,init(lastlabel)));
  262. if psetdef(p^.resulttype)^.settype=smallset then
  263. begin
  264. move(p^.value_set^,i,sizeof(longint));
  265. consts^.concat(new(pai_const,init_32bit(i)));
  266. end
  267. else
  268. begin
  269. for i:=0 to 31 do
  270. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  271. end;
  272. clear_reference(p^.location.reference);
  273. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_set));
  274. p^.location.loc:=LOC_MEM;
  275. {$endif SMALLSETORD}
  276. end;
  277. {*****************************************************************************
  278. SecondNilN
  279. *****************************************************************************}
  280. procedure secondniln(var p : ptree);
  281. begin
  282. p^.location.loc:=LOC_MEM;
  283. p^.location.reference.isintvalue:=true;
  284. p^.location.reference.offset:=0;
  285. end;
  286. end.
  287. {
  288. $Log$
  289. Revision 1.14 1998-09-17 09:42:13 peter
  290. + pass_2 for cg386
  291. * Message() -> CGMessage() for pass_1/pass_2
  292. Revision 1.13 1998/09/07 18:45:53 peter
  293. * update smartlinking, uses getdatalabel
  294. * renamed ptree.value vars to value_str,value_real,value_set
  295. Revision 1.12 1998/08/28 10:56:57 peter
  296. * removed warnings
  297. Revision 1.11 1998/08/14 18:18:39 peter
  298. + dynamic set contruction
  299. * smallsets are now working (always longint size)
  300. Revision 1.10 1998/08/04 13:22:46 pierre
  301. * weird bug fixed :
  302. a pchar ' ' (simple space or any other letter) was found to
  303. be equal to a string of length zero !!!
  304. thus printing out non sense
  305. found that out while checking Control-C !!
  306. + added column info also in RHIDE format as
  307. it might be usefull later
  308. Revision 1.9 1998/07/20 18:40:10 florian
  309. * handling of ansi string constants should now work
  310. Revision 1.8 1998/07/20 10:23:00 florian
  311. * better ansi string assignement
  312. Revision 1.7 1998/07/18 22:54:25 florian
  313. * some ansi/wide/longstring support fixed:
  314. o parameter passing
  315. o returning as result from functions
  316. Revision 1.6 1998/07/18 17:11:07 florian
  317. + ansi string constants fixed
  318. + switch $H partial implemented
  319. Revision 1.5 1998/06/25 08:48:07 florian
  320. * first version of rtti support
  321. Revision 1.4 1998/06/08 13:13:31 pierre
  322. + temporary variables now in temp_gen.pas unit
  323. because it is processor independent
  324. * mppc68k.bat modified to undefine i386 and support_mmx
  325. (which are defaults for i386)
  326. Revision 1.3 1998/06/05 17:44:11 peter
  327. * splitted cgi386
  328. Revision 1.2 1998/06/05 16:13:31 pierre
  329. * fix for real and string consts inside inlined procs
  330. Revision 1.1 1998/05/23 01:21:02 peter
  331. + aktasmmode, aktoptprocessor, aktoutputformat
  332. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  333. + $LIBNAME to set the library name where the unit will be put in
  334. * splitted cgi386 a bit (codeseg to large for bp7)
  335. * nasm, tasm works again. nasm moved to ag386nsm.pas
  336. }