1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /* 32bit target expressions:
24 Each calculation is performed three times using each of the
25 signed64, unsigned64 and long integer types. The macro ALU_END
26 (in _ALU_RESULT_VAL) then selects which of the three alternative
27 results will be used in the final assignment of the target
28 register. As this selection is determined at compile time by
29 fields in the instruction (OE, EA, Rc) the compiler has sufficient
30 information to firstly simplify the selection code into a single
31 case and then back anotate the equations and hence eliminate any
32 resulting dead code. That dead code being the calculations that,
33 as it turned out were not in the end needed.
35 64bit arrithemetic is used firstly because it allows the use of
36 gcc's efficient long long operators (typically efficiently output
37 inline) and secondly because the resultant answer will contain in
38 the low 32bits the answer while in the high 32bits is either carry
39 or status information. */
41 /* 64bit target expressions:
43 Unfortunatly 128bit arrithemetic isn't that common. Consequently
44 the 32/64 bit trick can not be used. Instead all calculations are
45 required to retain carry/overflow information in separate
46 variables. Even with this restriction it is still possible for the
47 trick of letting the compiler discard the calculation of unneeded
51 /* Macro's to type cast 32bit constants to 64bits */
52 #define SIGNED64(val) ((signed64)(signed32)(val))
53 #define UNSIGNED64(val) ((unsigned64)(unsigned32)(val))
56 /* Start a section of ALU code */
58 #define ALU_BEGIN(val) \
60 natural_word alu_val; \
61 unsigned64 alu_carry_val; \
62 signed64 alu_overflow_val; \
66 /* assign the result to the target register */
68 #define ALU_END(TARG,CA,OE,Rc) \
69 { /* select the result to use */ \
70 signed_word const alu_result = _ALU_RESULT_VAL(CA,OE,Rc); \
71 /* determine the overflow bit if needed */ \
73 if ((((unsigned64)(alu_overflow_val & BIT64(0))) \
75 == (alu_overflow_val & BIT64(32))) \
76 XER &= (~xer_overflow); \
78 XER |= (xer_summary_overflow | xer_overflow); \
80 /* Update the carry bit if needed */ \
82 XER = ((XER & ~xer_carry) \
83 | SHUFFLED32((alu_carry_val >> 32), 31, xer_carry_bit)); \
84 /* if (alu_carry_val & BIT64(31)) \
87 XER &= (~xer_carry); */ \
89 TRACE(trace_alu, (" Result = %ld (0x%lx), XER = %ld\n", \
90 (long)alu_result, (long)alu_result, (long)XER)); \
91 /* Update the Result Conditions if needed */ \
92 CR0_COMPARE(alu_result, 0, Rc); \
93 /* assign targ same */ \
97 /* select the result from the different options */
99 #define _ALU_RESULT_VAL(CA,OE,Rc) (WITH_TARGET_WORD_BITSIZE == 64 \
108 /* More basic alu operations */
109 #if (WITH_TARGET_WORD_BITSIZE == 64)
110 #define ALU_SET(val) \
113 alu_carry_val = ((unsigned64)alu_val) >> 32; \
114 alu_overflow_val = ((signed64)alu_val) >> 32; \
117 #if (WITH_TARGET_WORD_BITSIZE == 32)
118 #define ALU_SET(val) \
121 alu_carry_val = (unsigned32)(alu_val); \
122 alu_overflow_val = (signed32)(alu_val); \
126 #if (WITH_TARGET_WORD_BITSIZE == 64)
127 #define ALU_ADD(val) \
129 unsigned64 alu_lo = (UNSIGNED64(alu_val) \
130 + UNSIGNED64(val)); \
131 signed alu_carry = ((alu_lo & BIT(31)) != 0); \
132 alu_carry_val = (alu_carry_val \
133 + UNSIGNED64(EXTRACTED(val, 0, 31)) \
135 alu_overflow_val = (alu_overflow_val \
136 + SIGNED64(EXTRACTED(val, 0, 31)) \
138 alu_val = alu_val + val; \
141 #if (WITH_TARGET_WORD_BITSIZE == 32)
142 #define ALU_ADD(val) \
145 alu_carry_val += (unsigned32)(val); \
146 alu_overflow_val += (signed32)(val); \
151 #if (WITH_TARGET_WORD_BITSIZE == 64)
154 signed carry = MASKED32(XER, xer_carry_bit, xer_carry_bit) != 0; \
158 #if (WITH_TARGET_WORD_BITSIZE == 32)
161 signed carry = MASKED32(XER, xer_carry_bit, xer_carry_bit) != 0; \
168 #if (WITH_TARGET_WORD_BITSIZE == 64)
170 #if (WITH_TARGET_WORD_BITSIZE == 32)
171 #define ALU_SUB(val) \
174 alu_carry_val -= (unsigned32)(val); \
175 alu_overflow_val -= (signed32)(val); \
180 #if (WITH_TARGET_WORD_BITSIZE == 64)
182 #if (WITH_TARGET_WORD_BITSIZE == 32)
183 #define ALU_OR(val) \
186 alu_carry_val = (unsigned32)(alu_val); \
187 alu_overflow_val = (signed32)(alu_val); \
192 #if (WITH_TARGET_WORD_BITSIZE == 64)
194 #if (WITH_TARGET_WORD_BITSIZE == 32)
195 #define ALU_XOR(val) \
198 alu_carry_val = (unsigned32)(alu_val); \
199 alu_overflow_val = (signed32)(alu_val); \
205 #if (WITH_TARGET_WORD_BITSIZE == 64)
207 #if (WITH_TARGET_WORD_BITSIZE == 32)
210 alu_val = -alu_val; \
211 alu_carry_val = -alu_carry_val; \
212 alu_overflow_val = -alu_overflow_val; \
218 #if (WITH_TARGET_WORD_BITSIZE == 64)
220 #if (WITH_TARGET_WORD_BITSIZE == 32)
221 #define ALU_AND(val) \
224 alu_carry_val = (unsigned32)(alu_val); \
225 alu_overflow_val = (signed32)(alu_val); \
230 #if (WITH_TARGET_WORD_BITSIZE == 64)
233 signed64 new_alu_val = ~alu_val; \
234 ALU_SET(new_alu_val); \
237 #if (WITH_TARGET_WORD_BITSIZE == 32)
240 signed new_alu_val = ~alu_val; \
241 ALU_SET(new_alu_val); \
246 /* Macros for updating the condition register */
248 #define CR1_UPDATE(Rc) \
251 CR_SET(1, EXTRACTED32(FPSCR, fpscr_fx_bit, fpscr_ox_bit)); \
256 #define _DO_CR_COMPARE(LHS, RHS) \
263 #define CR_SET(REG, VAL) MBLIT32(CR, REG*4, REG*4+3, VAL)
264 #define CR_FIELD(REG) EXTRACTED32(CR, REG*4, REG*4+3)
265 #define CR_SET_XER_SO(REG, VAL) \
267 creg new_bits = ((XER & xer_summary_overflow) \
268 ? (cr_i_summary_overflow | VAL) \
270 CR_SET(REG, new_bits); \
273 #define CR_COMPARE(REG, LHS, RHS) \
275 creg new_bits = ((XER & xer_summary_overflow) \
276 ? (cr_i_summary_overflow | _DO_CR_COMPARE(LHS,RHS)) \
277 : _DO_CR_COMPARE(LHS,RHS)); \
278 CR_SET(REG, new_bits); \
281 #define CR0_COMPARE(LHS, RHS, Rc) \
284 CR_COMPARE(0, LHS, RHS); \
286 ("CR=0x%08lx, LHS=%ld, RHS=%ld\n", \
287 (unsigned long)CR, (long)LHS, (long)RHS)); \
293 /* Bring data in from the cold */
295 #define MEM(SIGN, EA, NR_BYTES) \
296 ((SIGN##_##NR_BYTES) vm_data_map_read_##NR_BYTES(cpu_data_map(processor), EA, \
299 #define STORE(EA, NR_BYTES, VAL) \
301 vm_data_map_write_##NR_BYTES(cpu_data_map(processor), EA, VAL, \
307 /* some FPSCR update macros. */
309 #define FPSCR_BEGIN \
311 fpscreg old_fpscr UNUSED = FPSCR
313 #define FPSCR_END(Rc) { \
314 /* always update VX */ \
315 if ((FPSCR & fpscr_vx_bits)) \
318 FPSCR &= ~fpscr_vx; \
319 /* always update FEX */ \
320 if (((FPSCR & fpscr_vx) && (FPSCR & fpscr_ve)) \
321 || ((FPSCR & fpscr_ox) && (FPSCR & fpscr_oe)) \
322 || ((FPSCR & fpscr_ux) && (FPSCR & fpscr_ue)) \
323 || ((FPSCR & fpscr_zx) && (FPSCR & fpscr_ze)) \
324 || ((FPSCR & fpscr_xx) && (FPSCR & fpscr_xe))) \
325 FPSCR |= fpscr_fex; \
327 FPSCR &= ~fpscr_fex; \
329 /* interrupt enabled? */ \
330 if ((MSR & (msr_floating_point_exception_mode_0 \
331 | msr_floating_point_exception_mode_1)) \
332 && (FPSCR & fpscr_fex)) \
333 program_interrupt(processor, cia, \
334 floating_point_enabled_program_interrupt); \
337 #define FPSCR_SET(REG, VAL) MBLIT32(FPSCR, REG*4, REG*4+3, VAL)
338 #define FPSCR_FIELD(REG) EXTRACTED32(FPSCR, REG*4, REG*4+3)
340 #define FPSCR_SET_FPCC(VAL) MBLIT32(FPSCR, fpscr_fpcc_bit, fpscr_fpcc_bit+3, VAL)
342 /* Handle various exceptions */
344 #define FPSCR_OR_VX(VAL) \
346 /* NOTE: VAL != 0 */ \
351 #define FPSCR_SET_OX(COND) \
358 FPSCR &= ~fpscr_ox; \
361 #define FPSCR_SET_UX(COND) \
368 FPSCR &= ~fpscr_ux; \
371 #define FPSCR_SET_ZX(COND) \
378 FPSCR &= ~fpscr_zx; \
381 #define FPSCR_SET_XX(COND) \
389 /* Note: code using SET_FI must also explicitly call SET_XX */
391 #define FPSCR_SET_FR(COND) do { \
395 FPSCR &= ~fpscr_fr; \
398 #define FPSCR_SET_FI(COND) \
404 FPSCR &= ~fpscr_fi; \
407 #define FPSCR_SET_FPRF(VAL) \
409 FPSCR = (FPSCR & ~fpscr_fprf) | (VAL); \