cg386con.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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,types,
  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,l1 : 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. { when changing that code, be careful that }
  137. { you don't use typed consts, which are }
  138. { are also written to consts }
  139. { currently, this is no problem, because }
  140. { typed consts have no leading length or }
  141. { they have no trailing zero }
  142. {$ifdef UseAnsiString}
  143. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  144. (pai_string(hp1)^.len=p^.length+2) then
  145. {$else UseAnsiString}
  146. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  147. (pai_string(hp1)^.len=length(p^.values^)+2) then
  148. {$endif UseAnsiString}
  149. begin
  150. same_string:=true;
  151. {$ifndef UseAnsiString}
  152. { weird error here !!! }
  153. { pchar ' ' was found equal to string '' !!!! }
  154. { gave strange output in exceptions !! PM }
  155. for i:=0 to length(p^.values^) do
  156. if pai_string(hp1)^.str[i]<>p^.values^[i] then
  157. {$else}
  158. for i:=0 to p^.length do
  159. if pai_string(hp1)^.str[i]<>p^.values[i] then
  160. {$endif}
  161. begin
  162. same_string:=false;
  163. break;
  164. end;
  165. if same_string then
  166. begin
  167. { found! }
  168. p^.labstrnumber:=lastlabel^.nb;
  169. break;
  170. end;
  171. end;
  172. lastlabel:=nil;
  173. end;
  174. hp1:=pai(hp1^.next);
  175. end;
  176. { :-(, we must generate a new entry }
  177. if p^.labstrnumber=-1 then
  178. begin
  179. getlabel(lastlabel);
  180. p^.labstrnumber:=lastlabel^.nb;
  181. {$ifndef UseAnsiString}
  182. getmem(pc,length(p^.values^)+3);
  183. move(p^.values^,pc^,length(p^.values^)+1);
  184. pc[length(p^.values^)+1]:=#0;
  185. concat_constlabel(lastlabel,conststring);
  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. {$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. concat_constlabel(lastlabel,conststring);
  195. if p^.length=0 then
  196. consts^.concat(new(pai_const,init_32bit(0)))
  197. else
  198. begin
  199. getlabel(l1);
  200. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  201. consts^.concat(new(pai_const,init_32bit(p^.length)));
  202. consts^.concat(new(pai_const,init_32bit(p^.length)));
  203. consts^.concat(new(pai_const,init_32bit(-1)));
  204. consts^.concat(new(pai_label,init(l1)));
  205. getmem(pc,p^.length+1);
  206. move(p^.values^,pc^,p^.length+1);
  207. { to overcome this problem we set the length explicitly }
  208. { with the ending null char }
  209. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  210. end;
  211. end;
  212. st_shortstring:
  213. begin
  214. getmem(pc,p^.length+3);
  215. move(p^.values^,pc[1],p^.length+1);
  216. pc[0]:=chr(p^.length);
  217. concat_constlabel(lastlabel,conststring);
  218. { to overcome this problem we set the length explicitly }
  219. { with the ending null char }
  220. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  221. end;
  222. end;
  223. {$endif UseAnsiString}
  224. end;
  225. end;
  226. stringdispose(p^.location.reference.symbol);
  227. if assigned(lastlabel) then
  228. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,conststring))
  229. else
  230. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labstrnumber,conststring));
  231. p^.location.loc := LOC_MEM;
  232. end;
  233. {*****************************************************************************
  234. SecondSetCons
  235. *****************************************************************************}
  236. procedure secondsetcons(var p : ptree);
  237. var
  238. l : plabel;
  239. i : longint;
  240. hp : ptree;
  241. href,sref : treference;
  242. {$ifdef TestSmallSet}
  243. smallsetvalue : longint;
  244. hr,hr2 : tregister;
  245. {$endif TestSmallSet}
  246. begin
  247. { this should be reimplemented for smallsets }
  248. { differently (PM) }
  249. { produce constant part }
  250. {$ifdef TestSmallSet}
  251. if psetdef(p^.resulttype)^.settype=smallset then
  252. begin
  253. smallsetvalue:=(p^.constset^[3]*256)+p^.constset^[2];
  254. smallsetvalue:=(smallsetvalue*256+p^.constset^[1])*256+p^.constset^[0];
  255. {consts^.concat(new(pai_const,init_32bit(smallsetvalue)));}
  256. hr:=getregister32;
  257. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  258. smallsetvalue,hr)));
  259. hp:=p^.left;
  260. if assigned(hp) then
  261. begin
  262. while assigned(hp) do
  263. begin
  264. secondpass(hp^.left);
  265. if codegenerror then
  266. exit;
  267. case hp^.left^.location.loc of
  268. LOC_MEM,LOC_REFERENCE :
  269. begin
  270. hr2:=getregister32;
  271. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  272. newreference(hp^.left^.location.reference),hr2)));
  273. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  274. hr2,hr)));
  275. ungetregister32(hr2);
  276. end;
  277. LOC_REGISTER,LOC_CREGISTER :
  278. exprasmlist^.concat(new(pai386,op_reg_reg(A_BTS,S_NO,
  279. hp^.left^.location.register,hr)));
  280. else
  281. internalerror(10567);
  282. end;
  283. hp:=hp^.right;
  284. end;
  285. end;
  286. p^.location.loc:=LOC_REGISTER;
  287. p^.location.register:=hr;
  288. end
  289. else
  290. {$endif TestSmallSet}
  291. begin
  292. href.symbol := Nil;
  293. clear_reference(href);
  294. getlabel(l);
  295. stringdispose(p^.location.reference.symbol);
  296. href.symbol:=stringdup(constlabel2str(l,constseta));
  297. concat_constlabel(l,constseta);
  298. for i:=0 to 31 do
  299. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  300. hp:=p^.left;
  301. if assigned(hp) then
  302. begin
  303. sref.symbol:=nil;
  304. gettempofsizereference(32,sref);
  305. concatcopy(href,sref,32,false);
  306. while assigned(hp) do
  307. begin
  308. secondpass(hp^.left);
  309. if codegenerror then
  310. exit;
  311. pushsetelement(hp^.left);
  312. emitpushreferenceaddr(exprasmlist,sref);
  313. { register is save in subroutine }
  314. emitcall('SET_SET_BYTE',true);
  315. hp:=hp^.right;
  316. end;
  317. p^.location.reference:=sref;
  318. end
  319. else
  320. p^.location.reference:=href;
  321. end;
  322. end;
  323. {*****************************************************************************
  324. SecondNilN
  325. *****************************************************************************}
  326. procedure secondniln(var p : ptree);
  327. begin
  328. p^.location.loc:=LOC_MEM;
  329. p^.location.reference.isintvalue:=true;
  330. p^.location.reference.offset:=0;
  331. end;
  332. end.
  333. {
  334. $Log$
  335. Revision 1.10 1998-08-04 13:22:46 pierre
  336. * weird bug fixed :
  337. a pchar ' ' (simple space or any other letter) was found to
  338. be equal to a string of length zero !!!
  339. thus printing out non sense
  340. found that out while checking Control-C !!
  341. + added column info also in RHIDE format as
  342. it might be usefull later
  343. Revision 1.9 1998/07/20 18:40:10 florian
  344. * handling of ansi string constants should now work
  345. Revision 1.8 1998/07/20 10:23:00 florian
  346. * better ansi string assignement
  347. Revision 1.7 1998/07/18 22:54:25 florian
  348. * some ansi/wide/longstring support fixed:
  349. o parameter passing
  350. o returning as result from functions
  351. Revision 1.6 1998/07/18 17:11:07 florian
  352. + ansi string constants fixed
  353. + switch $H partial implemented
  354. Revision 1.5 1998/06/25 08:48:07 florian
  355. * first version of rtti support
  356. Revision 1.4 1998/06/08 13:13:31 pierre
  357. + temporary variables now in temp_gen.pas unit
  358. because it is processor independent
  359. * mppc68k.bat modified to undefine i386 and support_mmx
  360. (which are defaults for i386)
  361. Revision 1.3 1998/06/05 17:44:11 peter
  362. * splitted cgi386
  363. Revision 1.2 1998/06/05 16:13:31 pierre
  364. * fix for real and string consts inside inlined procs
  365. Revision 1.1 1998/05/23 01:21:02 peter
  366. + aktasmmode, aktoptprocessor, aktoutputformat
  367. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  368. + $LIBNAME to set the library name where the unit will be put in
  369. * splitted cgi386 a bit (codeseg to large for bp7)
  370. * nasm, tasm works again. nasm moved to ag386nsm.pas
  371. }