cg386con.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 secondsetcons(var p : ptree);
  28. procedure secondniln(var p : ptree);
  29. implementation
  30. uses
  31. cobjects,verbose,globals,
  32. symtable,aasm,i386,types,
  33. hcodegen,cgai386,temp_gen,tgeni386,cgi386;
  34. {*****************************************************************************
  35. SecondRealConst
  36. *****************************************************************************}
  37. procedure secondrealconst(var p : ptree);
  38. var
  39. hp1 : pai;
  40. lastlabel : plabel;
  41. found : boolean;
  42. begin
  43. clear_reference(p^.location.reference);
  44. lastlabel:=nil;
  45. found:=false;
  46. { const already used ? }
  47. if p^.labnumber=-1 then
  48. begin
  49. { tries to found an old entry }
  50. hp1:=pai(consts^.first);
  51. while assigned(hp1) do
  52. begin
  53. if hp1^.typ=ait_label then
  54. lastlabel:=pai_label(hp1)^.l
  55. else
  56. begin
  57. if (hp1^.typ=p^.realtyp) and (lastlabel<>nil) then
  58. begin
  59. if ((p^.realtyp=ait_real_64bit) and (pai_double(hp1)^.value=p^.valued)) or
  60. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.valued)) or
  61. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.valued)) then
  62. begin
  63. { found! }
  64. p^.labnumber:=lastlabel^.nb;
  65. break;
  66. end;
  67. end;
  68. lastlabel:=nil;
  69. end;
  70. hp1:=pai(hp1^.next);
  71. end;
  72. { :-(, we must generate a new entry }
  73. if p^.labnumber=-1 then
  74. begin
  75. getlabel(lastlabel);
  76. p^.labnumber:=lastlabel^.nb;
  77. concat_constlabel(lastlabel,constreal);
  78. case p^.realtyp of
  79. ait_real_64bit : consts^.concat(new(pai_double,init(p^.valued)));
  80. ait_real_32bit : consts^.concat(new(pai_single,init(p^.valued)));
  81. ait_real_extended : consts^.concat(new(pai_extended,init(p^.valued)));
  82. else
  83. internalerror(10120);
  84. end;
  85. end;
  86. end;
  87. stringdispose(p^.location.reference.symbol);
  88. if assigned(lastlabel) then
  89. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,constreal))
  90. else
  91. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labnumber,constreal));
  92. end;
  93. {*****************************************************************************
  94. SecondFixConst
  95. *****************************************************************************}
  96. procedure secondfixconst(var p : ptree);
  97. begin
  98. { an fix comma const. behaves as a memory reference }
  99. p^.location.loc:=LOC_MEM;
  100. p^.location.reference.isintvalue:=true;
  101. p^.location.reference.offset:=p^.valuef;
  102. end;
  103. {*****************************************************************************
  104. SecondOrdConst
  105. *****************************************************************************}
  106. procedure secondordconst(var p : ptree);
  107. begin
  108. { an integer const. behaves as a memory reference }
  109. p^.location.loc:=LOC_MEM;
  110. p^.location.reference.isintvalue:=true;
  111. p^.location.reference.offset:=p^.value;
  112. end;
  113. {*****************************************************************************
  114. SecondStringConst
  115. *****************************************************************************}
  116. procedure secondstringconst(var p : ptree);
  117. var
  118. hp1 : pai;
  119. {$ifdef UseAnsiString}
  120. l1,
  121. {$endif}
  122. lastlabel : plabel;
  123. pc : pchar;
  124. same_string : boolean;
  125. i : word;
  126. begin
  127. clear_reference(p^.location.reference);
  128. lastlabel:=nil;
  129. { const already used ? }
  130. if p^.labstrnumber=-1 then
  131. begin
  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. {$ifdef UseAnsiString}
  147. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  148. (pai_string(hp1)^.len=p^.length+2) then
  149. {$else UseAnsiString}
  150. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  151. (pai_string(hp1)^.len=length(p^.values^)+2) then
  152. {$endif UseAnsiString}
  153. begin
  154. same_string:=true;
  155. {$ifndef UseAnsiString}
  156. { weird error here !!! }
  157. { pchar ' ' was found equal to string '' !!!! }
  158. { gave strange output in exceptions !! PM }
  159. for i:=0 to length(p^.values^) do
  160. if pai_string(hp1)^.str[i]<>p^.values^[i] then
  161. {$else}
  162. for i:=0 to p^.length do
  163. if pai_string(hp1)^.str[i]<>p^.values[i] then
  164. {$endif}
  165. begin
  166. same_string:=false;
  167. break;
  168. end;
  169. if same_string then
  170. begin
  171. { found! }
  172. p^.labstrnumber:=lastlabel^.nb;
  173. break;
  174. end;
  175. end;
  176. lastlabel:=nil;
  177. end;
  178. hp1:=pai(hp1^.next);
  179. end;
  180. { :-(, we must generate a new entry }
  181. if p^.labstrnumber=-1 then
  182. begin
  183. getlabel(lastlabel);
  184. p^.labstrnumber:=lastlabel^.nb;
  185. {$ifndef UseAnsiString}
  186. getmem(pc,length(p^.values^)+3);
  187. move(p^.values^,pc^,length(p^.values^)+1);
  188. pc[length(p^.values^)+1]:=#0;
  189. concat_constlabel(lastlabel,conststring);
  190. { we still will have a problem if there is a #0 inside the pchar }
  191. consts^.concat(new(pai_string,init_length_pchar(pc,length(p^.values^)+2)));
  192. {$else UseAnsiString}
  193. { generate an ansi string ? }
  194. case p^.stringtype of
  195. st_ansistring:
  196. begin
  197. { an empty ansi string is nil! }
  198. concat_constlabel(lastlabel,conststring);
  199. if p^.length=0 then
  200. consts^.concat(new(pai_const,init_32bit(0)))
  201. else
  202. begin
  203. getlabel(l1);
  204. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  205. consts^.concat(new(pai_const,init_32bit(p^.length)));
  206. consts^.concat(new(pai_const,init_32bit(p^.length)));
  207. consts^.concat(new(pai_const,init_32bit(-1)));
  208. consts^.concat(new(pai_label,init(l1)));
  209. getmem(pc,p^.length+1);
  210. move(p^.values^,pc^,p^.length+1);
  211. { to overcome this problem we set the length explicitly }
  212. { with the ending null char }
  213. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  214. end;
  215. end;
  216. st_shortstring:
  217. begin
  218. getmem(pc,p^.length+3);
  219. move(p^.values^,pc[1],p^.length+1);
  220. pc[0]:=chr(p^.length);
  221. concat_constlabel(lastlabel,conststring);
  222. { to overcome this problem we set the length explicitly }
  223. { with the ending null char }
  224. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  225. end;
  226. end;
  227. {$endif UseAnsiString}
  228. end;
  229. end;
  230. stringdispose(p^.location.reference.symbol);
  231. if assigned(lastlabel) then
  232. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,conststring))
  233. else
  234. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labstrnumber,conststring));
  235. p^.location.loc := LOC_MEM;
  236. end;
  237. {*****************************************************************************
  238. SecondSetCons
  239. *****************************************************************************}
  240. procedure secondsetcons(var p : ptree);
  241. var
  242. l : plabel;
  243. i : longint;
  244. href : treference;
  245. begin
  246. {$ifdef SMALLSETORD}
  247. if psetdef(p^.resulttype)^.settype=smallset then
  248. begin
  249. p^.location.loc:=LOC_MEM;
  250. p^.location.reference.isintvalue:=true;
  251. p^.location.reference.offset:=p^.constset^[0];
  252. end
  253. else
  254. begin
  255. reset_reference(href);
  256. getlabel(l);
  257. stringdispose(p^.location.reference.symbol);
  258. href.symbol:=stringdup(constlabel2str(l,constseta));
  259. concat_constlabel(l,constseta);
  260. for i:=0 to 31 do
  261. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  262. p^.location.reference:=href;
  263. end;
  264. {$else}
  265. reset_reference(href);
  266. getlabel(l);
  267. stringdispose(p^.location.reference.symbol);
  268. href.symbol:=stringdup(constlabel2str(l,constseta));
  269. concat_constlabel(l,constseta);
  270. if psetdef(p^.resulttype)^.settype=smallset then
  271. begin
  272. move(p^.constset^,i,sizeof(longint));
  273. consts^.concat(new(pai_const,init_32bit(i)));
  274. end
  275. else
  276. begin
  277. for i:=0 to 31 do
  278. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  279. end;
  280. p^.location.reference:=href;
  281. {$endif SMALLSETORD}
  282. end;
  283. {*****************************************************************************
  284. SecondNilN
  285. *****************************************************************************}
  286. procedure secondniln(var p : ptree);
  287. begin
  288. p^.location.loc:=LOC_MEM;
  289. p^.location.reference.isintvalue:=true;
  290. p^.location.reference.offset:=0;
  291. end;
  292. end.
  293. {
  294. $Log$
  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. }