cg68kcon.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate m68k 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 cg68kcon;
  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. globtype,systems,
  32. cobjects,verbose,globals,
  33. symtable,aasm,types,
  34. hcodegen,temp_gen,pass_2,
  35. cpubase,cga68k,tgen68k,symconst;
  36. {*****************************************************************************
  37. SecondRealConst
  38. *****************************************************************************}
  39. procedure secondrealconst(var p : ptree);
  40. const
  41. floattype2ait:array[tfloattype] of tait=
  42. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_none,ait_none);
  43. var
  44. hp1 : pai;
  45. lastlabel : pasmlabel;
  46. realait : tait;
  47. begin
  48. lastlabel:=nil;
  49. realait:=floattype2ait[pfloatdef(p^.resulttype)^.typ];
  50. { const already used ? }
  51. if not assigned(p^.lab_real) then
  52. begin
  53. { tries to found an old entry }
  54. hp1:=pai(consts^.first);
  55. while assigned(hp1) do
  56. begin
  57. if hp1^.typ=ait_label then
  58. lastlabel:=pai_label(hp1)^.l
  59. else
  60. begin
  61. if (hp1^.typ=realait) and (lastlabel<>nil) then
  62. begin
  63. if(
  64. ((realait=ait_real_32bit) and (pai_real_32bit(hp1)^.value=p^.value_real)) or
  65. ((realait=ait_real_64bit) and (pai_real_64bit(hp1)^.value=p^.value_real)) or
  66. ((realait=ait_real_80bit) and (pai_real_80bit(hp1)^.value=p^.value_real)) or
  67. ((realait=ait_comp_64bit) and (pai_comp_64bit(hp1)^.value=p^.value_real))
  68. ) then
  69. begin
  70. { found! }
  71. p^.lab_real:=lastlabel;
  72. break;
  73. end;
  74. end;
  75. lastlabel:=nil;
  76. end;
  77. hp1:=pai(hp1^.next);
  78. end;
  79. { :-(, we must generate a new entry }
  80. if not assigned(p^.lab_real) then
  81. begin
  82. getdatalabel(lastlabel);
  83. p^.lab_real:=lastlabel;
  84. if (cs_create_smart in aktmoduleswitches) then
  85. consts^.concat(new(pai_cut,init));
  86. consts^.concat(new(pai_label,init(lastlabel)));
  87. case realait of
  88. ait_real_64bit : consts^.concat(new(pai_real_32bit,init(p^.value_real)));
  89. ait_real_32bit : consts^.concat(new(pai_real_32bit,init(p^.value_real)));
  90. ait_real_80bit : consts^.concat(new(pai_real_32bit,init(p^.value_real)));
  91. else
  92. internalerror(10120);
  93. end;
  94. end;
  95. end;
  96. clear_reference(p^.location.reference);
  97. p^.location.reference.symbol:=stringdup(p^.lab_real^.name);
  98. p^.location.loc:=LOC_MEM;
  99. end;
  100. {*****************************************************************************
  101. SecondFixConst
  102. *****************************************************************************}
  103. procedure secondfixconst(var p : ptree);
  104. begin
  105. { an fix comma const. behaves as a memory reference }
  106. p^.location.loc:=LOC_MEM;
  107. p^.location.reference.isintvalue:=true;
  108. p^.location.reference.offset:=p^.value_fix;
  109. end;
  110. {*****************************************************************************
  111. SecondOrdConst
  112. *****************************************************************************}
  113. procedure secondordconst(var p : ptree);
  114. begin
  115. { an integer const. behaves as a memory reference }
  116. p^.location.loc:=LOC_MEM;
  117. p^.location.reference.isintvalue:=true;
  118. p^.location.reference.offset:=p^.value;
  119. end;
  120. {*****************************************************************************
  121. SecondStringConst
  122. *****************************************************************************}
  123. procedure secondstringconst(var p : ptree);
  124. var
  125. hp1 : pai;
  126. l1,l2,
  127. lastlabel : pasmlabel;
  128. pc : pchar;
  129. same_string : boolean;
  130. i,mylength : longint;
  131. begin
  132. lastlabel:=nil;
  133. { const already used ? }
  134. if not assigned(p^.lab_str) then
  135. begin
  136. if is_shortstring(p^.resulttype) then
  137. mylength:=p^.length+2
  138. else
  139. mylength:=p^.length+1;
  140. { tries to found an old entry }
  141. hp1:=pai(consts^.first);
  142. while assigned(hp1) do
  143. begin
  144. if hp1^.typ=ait_label then
  145. lastlabel:=pai_label(hp1)^.l
  146. else
  147. begin
  148. { when changing that code, be careful that }
  149. { you don't use typed consts, which are }
  150. { are also written to consts }
  151. { currently, this is no problem, because }
  152. { typed consts have no leading length or }
  153. { they have no trailing zero }
  154. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  155. (pai_string(hp1)^.len=mylength) then
  156. begin
  157. same_string:=true;
  158. for i:=0 to p^.length do
  159. if pai_string(hp1)^.str[i]<>p^.value_str[i] then
  160. begin
  161. same_string:=false;
  162. break;
  163. end;
  164. if same_string then
  165. begin
  166. { found! }
  167. p^.lab_str:=lastlabel;
  168. break;
  169. end;
  170. end;
  171. lastlabel:=nil;
  172. end;
  173. hp1:=pai(hp1^.next);
  174. end;
  175. { :-(, we must generate a new entry }
  176. if not assigned(p^.lab_str) then
  177. begin
  178. getdatalabel(lastlabel);
  179. p^.lab_str:=lastlabel;
  180. if (cs_create_smart in aktmoduleswitches) then
  181. consts^.concat(new(pai_cut,init));
  182. consts^.concat(new(pai_label,init(lastlabel)));
  183. { generate an ansi string ? }
  184. case p^.stringtype of
  185. st_ansistring:
  186. begin
  187. { an empty ansi string is nil! }
  188. if p^.length=0 then
  189. consts^.concat(new(pai_const,init_32bit(0)))
  190. else
  191. begin
  192. getdatalabel(l1);
  193. getdatalabel(l2);
  194. consts^.concat(new(pai_label,init(l2)));
  195. consts^.concat(new(pai_const_symbol,init(l1)));
  196. consts^.concat(new(pai_const,init_32bit(p^.length)));
  197. consts^.concat(new(pai_const,init_32bit(p^.length)));
  198. consts^.concat(new(pai_const,init_32bit(-1)));
  199. consts^.concat(new(pai_label,init(l1)));
  200. getmem(pc,p^.length+2);
  201. move(p^.value_str^,pc^,p^.length);
  202. pc[p^.length]:=#0;
  203. { to overcome this problem we set the length explicitly }
  204. { with the ending null char }
  205. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  206. { return the offset of the real string }
  207. p^.lab_str:=l2;
  208. end;
  209. end;
  210. st_shortstring:
  211. begin
  212. { empty strings }
  213. if p^.length=0 then
  214. consts^.concat(new(pai_const,init_16bit(0)))
  215. else
  216. begin
  217. { also length and terminating zero }
  218. getmem(pc,p^.length+3);
  219. move(p^.value_str^,pc[1],p^.length+1);
  220. pc[0]:=chr(p^.length);
  221. { to overcome this problem we set the length explicitly }
  222. { with the ending null char }
  223. pc[p^.length+1]:=#0;
  224. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  225. end;
  226. end;
  227. end;
  228. end;
  229. end;
  230. clear_reference(p^.location.reference);
  231. p^.location.reference.symbol:=stringdup(p^.lab_str^.name);
  232. p^.location.loc:=LOC_MEM;
  233. end;
  234. {*****************************************************************************
  235. SecondSetCons
  236. *****************************************************************************}
  237. procedure secondsetconst(var p : ptree);
  238. var
  239. hp1 : pai;
  240. lastlabel : pasmlabel;
  241. i : longint;
  242. neededtyp : tait;
  243. begin
  244. {$ifdef SMALLSETORD}
  245. { small sets are loaded as constants }
  246. if psetdef(p^.resulttype)^.settype=smallset then
  247. begin
  248. p^.location.loc:=LOC_MEM;
  249. p^.location.reference.isintvalue:=true;
  250. p^.location.reference.offset:=plongint(p^.value_set)^;
  251. exit;
  252. end;
  253. {$endif}
  254. if psetdef(p^.resulttype)^.settype=smallset then
  255. neededtyp:=ait_const_32bit
  256. else
  257. neededtyp:=ait_const_8bit;
  258. lastlabel:=nil;
  259. { const already used ? }
  260. if not assigned(p^.lab_set) then
  261. begin
  262. { tries to found an old entry }
  263. hp1:=pai(consts^.first);
  264. while assigned(hp1) do
  265. begin
  266. if hp1^.typ=ait_label then
  267. lastlabel:=pai_label(hp1)^.l
  268. else
  269. begin
  270. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  271. begin
  272. if (hp1^.typ=ait_const_8bit) then
  273. begin
  274. { compare normal set }
  275. i:=0;
  276. while assigned(hp1) and (i<32) do
  277. begin
  278. if pai_const(hp1)^.value<>p^.value_set^[i] then
  279. break;
  280. inc(i);
  281. hp1:=pai(hp1^.next);
  282. end;
  283. if i=32 then
  284. begin
  285. { found! }
  286. p^.lab_set:=lastlabel;
  287. break;
  288. end;
  289. { leave when the end of consts is reached, so no
  290. hp1^.next is done }
  291. if not assigned(hp1) then
  292. break;
  293. end
  294. else
  295. begin
  296. { compare small set }
  297. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  298. begin
  299. { found! }
  300. p^.lab_set:=lastlabel;
  301. break;
  302. end;
  303. end;
  304. end;
  305. lastlabel:=nil;
  306. end;
  307. hp1:=pai(hp1^.next);
  308. end;
  309. { :-(, we must generate a new entry }
  310. if not assigned(p^.lab_set) then
  311. begin
  312. getdatalabel(lastlabel);
  313. p^.lab_set:=lastlabel;
  314. if (cs_create_smart in aktmoduleswitches) then
  315. consts^.concat(new(pai_cut,init));
  316. consts^.concat(new(pai_label,init(lastlabel)));
  317. if psetdef(p^.resulttype)^.settype=smallset then
  318. begin
  319. move(p^.value_set^,i,sizeof(longint));
  320. consts^.concat(new(pai_const,init_32bit(i)));
  321. end
  322. else
  323. begin
  324. for i:=0 to 31 do
  325. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  326. end;
  327. end;
  328. end;
  329. clear_reference(p^.location.reference);
  330. p^.location.reference.symbol:=stringdup(p^.lab_set^.name);
  331. p^.location.loc:=LOC_MEM;
  332. end;
  333. {*****************************************************************************
  334. SecondNilN
  335. *****************************************************************************}
  336. procedure secondniln(var p : ptree);
  337. begin
  338. p^.location.loc:=LOC_MEM;
  339. p^.location.reference.isintvalue:=true;
  340. p^.location.reference.offset:=0;
  341. end;
  342. end.
  343. {
  344. $Log$
  345. Revision 1.9 2000-02-09 13:22:49 peter
  346. * log truncated
  347. Revision 1.8 2000/01/07 01:14:22 peter
  348. * updated copyright to 2000
  349. Revision 1.7 1999/09/20 16:38:52 peter
  350. * cs_create_smart instead of cs_smartlink
  351. * -CX is create smartlink
  352. * -CD is create dynamic, but does nothing atm.
  353. Revision 1.6 1999/09/16 23:05:51 florian
  354. * m68k compiler is again compilable (only gas writer, no assembler reader)
  355. }