cg386con.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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,
  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. {$else UseAnsiString}
  172. pc:=getpcharcopy(p);
  173. {$endif UseAnsiString}
  174. concat_constlabel(lastlabel,conststring);
  175. {$ifdef UseAnsiString}
  176. {$ifdef debug}
  177. consts^.concat(new(pai_asm_comment,init('Header of ansistring')));
  178. {$endif debug}
  179. consts^.concat(new(pai_const,init_32bit(p^.length)));
  180. consts^.concat(new(pai_const,init_32bit(p^.length)));
  181. consts^.concat(new(pai_const,init_32bit(-1)));
  182. { to overcome this problem we set the length explicitly }
  183. { with the ending null char }
  184. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  185. {$else UseAnsiString}
  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^.values^)+2)));
  188. {$endif UseAnsiString}
  189. end;
  190. end;
  191. stringdispose(p^.location.reference.symbol);
  192. if assigned(lastlabel) then
  193. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,conststring))
  194. else
  195. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labstrnumber,conststring));
  196. p^.location.loc := LOC_MEM;
  197. end;
  198. {*****************************************************************************
  199. SecondSetCons
  200. *****************************************************************************}
  201. procedure secondsetcons(var p : ptree);
  202. var
  203. l : plabel;
  204. i : longint;
  205. hp : ptree;
  206. href,sref : treference;
  207. {$ifdef TestSmallSet}
  208. smallsetvalue : longint;
  209. hr,hr2 : tregister;
  210. {$endif TestSmallSet}
  211. begin
  212. { this should be reimplemented for smallsets }
  213. { differently (PM) }
  214. { produce constant part }
  215. {$ifdef TestSmallSet}
  216. if psetdef(p^.resulttype)^.settype=smallset then
  217. begin
  218. smallsetvalue:=(p^.constset^[3]*256)+p^.constset^[2];
  219. smallsetvalue:=(smallsetvalue*256+p^.constset^[1])*256+p^.constset^[0];
  220. {consts^.concat(new(pai_const,init_32bit(smallsetvalue)));}
  221. hr:=getregister32;
  222. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  223. smallsetvalue,hr)));
  224. hp:=p^.left;
  225. if assigned(hp) then
  226. begin
  227. while assigned(hp) do
  228. begin
  229. secondpass(hp^.left);
  230. if codegenerror then
  231. exit;
  232. case hp^.left^.location.loc of
  233. LOC_MEM,LOC_REFERENCE :
  234. begin
  235. hr2:=getregister32;
  236. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  237. newreference(hp^.left^.location.reference),hr2)));
  238. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  239. hr2,hr)));
  240. ungetregister32(hr2);
  241. end;
  242. LOC_REGISTER,LOC_CREGISTER :
  243. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  244. hp^.left^.location.register,hr)));
  245. else
  246. internalerror(10567);
  247. end;
  248. hp:=hp^.right;
  249. end;
  250. end;
  251. p^.location.loc:=LOC_REGISTER;
  252. p^.location.register:=hr;
  253. end
  254. else
  255. {$endif TestSmallSet}
  256. begin
  257. href.symbol := Nil;
  258. clear_reference(href);
  259. getlabel(l);
  260. stringdispose(p^.location.reference.symbol);
  261. href.symbol:=stringdup(constlabel2str(l,constseta));
  262. concat_constlabel(l,constseta);
  263. for i:=0 to 31 do
  264. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  265. hp:=p^.left;
  266. if assigned(hp) then
  267. begin
  268. sref.symbol:=nil;
  269. gettempofsizereference(32,sref);
  270. concatcopy(href,sref,32,false);
  271. while assigned(hp) do
  272. begin
  273. secondpass(hp^.left);
  274. if codegenerror then
  275. exit;
  276. pushsetelement(hp^.left);
  277. emitpushreferenceaddr(sref);
  278. { register is save in subroutine }
  279. emitcall('SET_SET_BYTE',true);
  280. hp:=hp^.right;
  281. end;
  282. p^.location.reference:=sref;
  283. end
  284. else
  285. p^.location.reference:=href;
  286. end;
  287. end;
  288. {*****************************************************************************
  289. SecondNilN
  290. *****************************************************************************}
  291. procedure secondniln(var p : ptree);
  292. begin
  293. p^.location.loc:=LOC_MEM;
  294. p^.location.reference.isintvalue:=true;
  295. p^.location.reference.offset:=0;
  296. end;
  297. end.
  298. {
  299. $Log$
  300. Revision 1.4 1998-06-08 13:13:31 pierre
  301. + temporary variables now in temp_gen.pas unit
  302. because it is processor independent
  303. * mppc68k.bat modified to undefine i386 and support_mmx
  304. (which are defaults for i386)
  305. Revision 1.3 1998/06/05 17:44:11 peter
  306. * splitted cgi386
  307. Revision 1.2 1998/06/05 16:13:31 pierre
  308. * fix for real and string consts inside inlined procs
  309. Revision 1.1 1998/05/23 01:21:02 peter
  310. + aktasmmode, aktoptprocessor, aktoutputformat
  311. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  312. + $LIBNAME to set the library name where the unit will be put in
  313. * splitted cgi386 a bit (codeseg to large for bp7)
  314. * nasm, tasm works again. nasm moved to ag386nsm.pas
  315. }