cg386con.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. procedure secondrealconst(var p : ptree);
  23. procedure secondfixconst(var p : ptree);
  24. procedure secondordconst(var p : ptree);
  25. procedure secondstringconst(var p : ptree);
  26. procedure secondsetcons(var p : ptree);
  27. procedure secondniln(var p : ptree);
  28. implementation
  29. uses
  30. cobjects,verbose,globals,
  31. symtable,aasm,i386,
  32. hcodegen,cgai386,temp_gen,tgeni386,cgi386;
  33. {*****************************************************************************
  34. SecondRealConst
  35. *****************************************************************************}
  36. procedure secondrealconst(var p : ptree);
  37. var
  38. hp1 : pai;
  39. lastlabel : plabel;
  40. found : boolean;
  41. begin
  42. clear_reference(p^.location.reference);
  43. lastlabel:=nil;
  44. found:=false;
  45. { const already used ? }
  46. if p^.labnumber=-1 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^.valued)) or
  59. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.valued)) or
  60. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.valued)) then
  61. begin
  62. { found! }
  63. p^.labnumber:=lastlabel^.nb;
  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 p^.labnumber=-1 then
  73. begin
  74. getlabel(lastlabel);
  75. p^.labnumber:=lastlabel^.nb;
  76. concat_constlabel(lastlabel,constreal);
  77. case p^.realtyp of
  78. ait_real_64bit : consts^.concat(new(pai_double,init(p^.valued)));
  79. ait_real_32bit : consts^.concat(new(pai_single,init(p^.valued)));
  80. ait_real_extended : consts^.concat(new(pai_extended,init(p^.valued)));
  81. else
  82. internalerror(10120);
  83. end;
  84. end;
  85. end;
  86. stringdispose(p^.location.reference.symbol);
  87. if assigned(lastlabel) then
  88. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,constreal))
  89. else
  90. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labnumber,constreal));
  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^.valuef;
  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. lastlabel : plabel;
  119. pc : pchar;
  120. same_string : boolean;
  121. i : word;
  122. begin
  123. clear_reference(p^.location.reference);
  124. lastlabel:=nil;
  125. { const already used ? }
  126. if p^.labstrnumber=-1 then
  127. begin
  128. { tries to found an old entry }
  129. hp1:=pai(consts^.first);
  130. while assigned(hp1) do
  131. begin
  132. if hp1^.typ=ait_label then
  133. lastlabel:=pai_label(hp1)^.l
  134. else
  135. begin
  136. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  137. (pai_string(hp1)^.len=length(p^.values^)+2) then
  138. begin
  139. same_string:=true;
  140. {$ifndef UseAnsiString}
  141. for i:=1 to length(p^.values^) do
  142. if pai_string(hp1)^.str[i]<>p^.values^[i] then
  143. {$else}
  144. for i:=0 to p^.length do
  145. if pai_string(hp1)^.str[i]<>p^.values[i] then
  146. {$endif}
  147. begin
  148. same_string:=false;
  149. break;
  150. end;
  151. if same_string then
  152. begin
  153. { found! }
  154. p^.labstrnumber:=lastlabel^.nb;
  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 p^.labstrnumber=-1 then
  164. begin
  165. getlabel(lastlabel);
  166. p^.labstrnumber:=lastlabel^.nb;
  167. {$ifndef UseAnsiString}
  168. getmem(pc,length(p^.values^)+3);
  169. move(p^.values^,pc^,length(p^.values^)+1);
  170. pc[length(p^.values^)+1]:=#0;
  171. concat_constlabel(lastlabel,conststring);
  172. { we still will have a problem if there is a #0 inside the pchar }
  173. consts^.concat(new(pai_string,init_length_pchar(pc,length(p^.values^)+2)));
  174. {$else UseAnsiString}
  175. if cs_ansistrings in aktswitches then
  176. begin
  177. concat_constlabel(lastlabel,conststring);
  178. consts^.concat(new(pai_const,init_32bit(p^.length)));
  179. consts^.concat(new(pai_const,init_32bit(p^.length)));
  180. consts^.concat(new(pai_const,init_32bit(-1)));
  181. getmem(pc,p^.length+1);
  182. move(p^.values^,pc^,p^.length+1);
  183. { to overcome this problem we set the length explicitly }
  184. { with the ending null char }
  185. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  186. end
  187. else
  188. begin
  189. getmem(pc,p^.length+3);
  190. move(p^.values^,pc[1],p^.length+1);
  191. pc[0]:=chr(p^.length);
  192. concat_constlabel(lastlabel,conststring);
  193. { to overcome this problem we set the length explicitly }
  194. { with the ending null char }
  195. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  196. end;
  197. {$endif UseAnsiString}
  198. end;
  199. end;
  200. stringdispose(p^.location.reference.symbol);
  201. if assigned(lastlabel) then
  202. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,conststring))
  203. else
  204. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labstrnumber,conststring));
  205. p^.location.loc := LOC_MEM;
  206. end;
  207. {*****************************************************************************
  208. SecondSetCons
  209. *****************************************************************************}
  210. procedure secondsetcons(var p : ptree);
  211. var
  212. l : plabel;
  213. i : longint;
  214. hp : ptree;
  215. href,sref : treference;
  216. {$ifdef TestSmallSet}
  217. smallsetvalue : longint;
  218. hr,hr2 : tregister;
  219. {$endif TestSmallSet}
  220. begin
  221. { this should be reimplemented for smallsets }
  222. { differently (PM) }
  223. { produce constant part }
  224. {$ifdef TestSmallSet}
  225. if psetdef(p^.resulttype)^.settype=smallset then
  226. begin
  227. smallsetvalue:=(p^.constset^[3]*256)+p^.constset^[2];
  228. smallsetvalue:=(smallsetvalue*256+p^.constset^[1])*256+p^.constset^[0];
  229. {consts^.concat(new(pai_const,init_32bit(smallsetvalue)));}
  230. hr:=getregister32;
  231. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  232. smallsetvalue,hr)));
  233. hp:=p^.left;
  234. if assigned(hp) then
  235. begin
  236. while assigned(hp) do
  237. begin
  238. secondpass(hp^.left);
  239. if codegenerror then
  240. exit;
  241. case hp^.left^.location.loc of
  242. LOC_MEM,LOC_REFERENCE :
  243. begin
  244. hr2:=getregister32;
  245. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  246. newreference(hp^.left^.location.reference),hr2)));
  247. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  248. hr2,hr)));
  249. ungetregister32(hr2);
  250. end;
  251. LOC_REGISTER,LOC_CREGISTER :
  252. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  253. hp^.left^.location.register,hr)));
  254. else
  255. internalerror(10567);
  256. end;
  257. hp:=hp^.right;
  258. end;
  259. end;
  260. p^.location.loc:=LOC_REGISTER;
  261. p^.location.register:=hr;
  262. end
  263. else
  264. {$endif TestSmallSet}
  265. begin
  266. href.symbol := Nil;
  267. clear_reference(href);
  268. getlabel(l);
  269. stringdispose(p^.location.reference.symbol);
  270. href.symbol:=stringdup(constlabel2str(l,constseta));
  271. concat_constlabel(l,constseta);
  272. for i:=0 to 31 do
  273. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  274. hp:=p^.left;
  275. if assigned(hp) then
  276. begin
  277. sref.symbol:=nil;
  278. gettempofsizereference(32,sref);
  279. concatcopy(href,sref,32,false);
  280. while assigned(hp) do
  281. begin
  282. secondpass(hp^.left);
  283. if codegenerror then
  284. exit;
  285. pushsetelement(hp^.left);
  286. emitpushreferenceaddr(exprasmlist,sref);
  287. { register is save in subroutine }
  288. emitcall('SET_SET_BYTE',true);
  289. hp:=hp^.right;
  290. end;
  291. p^.location.reference:=sref;
  292. end
  293. else
  294. p^.location.reference:=href;
  295. end;
  296. end;
  297. {*****************************************************************************
  298. SecondNilN
  299. *****************************************************************************}
  300. procedure secondniln(var p : ptree);
  301. begin
  302. p^.location.loc:=LOC_MEM;
  303. p^.location.reference.isintvalue:=true;
  304. p^.location.reference.offset:=0;
  305. end;
  306. end.
  307. {
  308. $Log$
  309. Revision 1.6 1998-07-18 17:11:07 florian
  310. + ansi string constants fixed
  311. + switch $H partial implemented
  312. Revision 1.5 1998/06/25 08:48:07 florian
  313. * first version of rtti support
  314. Revision 1.4 1998/06/08 13:13:31 pierre
  315. + temporary variables now in temp_gen.pas unit
  316. because it is processor independent
  317. * mppc68k.bat modified to undefine i386 and support_mmx
  318. (which are defaults for i386)
  319. Revision 1.3 1998/06/05 17:44:11 peter
  320. * splitted cgi386
  321. Revision 1.2 1998/06/05 16:13:31 pierre
  322. * fix for real and string consts inside inlined procs
  323. Revision 1.1 1998/05/23 01:21:02 peter
  324. + aktasmmode, aktoptprocessor, aktoutputformat
  325. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  326. + $LIBNAME to set the library name where the unit will be put in
  327. * splitted cgi386 a bit (codeseg to large for bp7)
  328. * nasm, tasm works again. nasm moved to ag386nsm.pas
  329. }