1 /* This file is part of the program psim.
3 Copyright 1994, 1995, 1996, 1997, 2003 Andrew Cagney
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 3 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, see <http://www.gnu.org/licenses/>.
22 /* Additional, and optional expressions. */
24 #include "altivec_expression.h"
27 #include "e500_expression.h"
30 /* 32bit target expressions:
32 Each calculation is performed three times using each of the
33 int64_t, uint64_t and long integer types. The macro ALU_END
34 (in _ALU_RESULT_VAL) then selects which of the three alternative
35 results will be used in the final assignment of the target
36 register. As this selection is determined at compile time by
37 fields in the instruction (OE, EA, Rc) the compiler has sufficient
38 information to firstly simplify the selection code into a single
39 case and then back anotate the equations and hence eliminate any
40 resulting dead code. That dead code being the calculations that,
41 as it turned out were not in the end needed.
43 64bit arrithemetic is used firstly because it allows the use of
44 gcc's efficient long long operators (typically efficiently output
45 inline) and secondly because the resultant answer will contain in
46 the low 32bits the answer while in the high 32bits is either carry
47 or status information. */
49 /* 64bit target expressions:
51 Unfortunatly 128bit arrithemetic isn't that common. Consequently
52 the 32/64 bit trick can not be used. Instead all calculations are
53 required to retain carry/overflow information in separate
54 variables. Even with this restriction it is still possible for the
55 trick of letting the compiler discard the calculation of unneeded
59 /* Macro's to type cast 32bit constants to 64bits */
60 #define ALU_SIGNED64(val) ((int64_t)(int32_t)(val))
61 #define ALU_UNSIGNED64(val) ((uint64_t)(uint32_t)(val))
64 /* Start a section of ALU code */
66 #define ALU_BEGIN(val) \
68 signed_word alu_val; \
69 uint64_t alu_carry_val; \
70 int64_t alu_overflow_val; \
74 /* assign the result to the target register */
76 #define ALU_END(TARG,CA,OE,Rc) \
77 { /* select the result to use */ \
78 signed_word const alu_result = _ALU_RESULT_VAL(CA,OE,Rc); \
79 /* determine the overflow bit if needed */ \
81 if ((((uint64_t)(alu_overflow_val & BIT64(0))) \
83 == (alu_overflow_val & BIT64(32))) \
84 XER &= (~xer_overflow); \
86 XER |= (xer_summary_overflow | xer_overflow); \
88 /* Update the carry bit if needed */ \
90 XER = ((XER & ~xer_carry) \
91 | SHUFFLED32((alu_carry_val >> 32), 31, xer_carry_bit)); \
92 /* if (alu_carry_val & BIT64(31)) \
95 XER &= (~xer_carry); */ \
97 TRACE(trace_alu, (" Result = %ld (0x%lx), XER = %ld\n", \
98 (long)alu_result, (long)alu_result, (long)XER)); \
99 /* Update the Result Conditions if needed */ \
100 CR0_COMPARE(alu_result, 0, Rc); \
101 /* assign targ same */ \
105 /* select the result from the different options */
107 #define _ALU_RESULT_VAL(CA,OE,Rc) (WITH_TARGET_WORD_BITSIZE == 64 \
116 /* More basic alu operations */
117 #if (WITH_TARGET_WORD_BITSIZE == 64)
118 #define ALU_SET(val) \
121 alu_carry_val = ((uint64_t)alu_val) >> 32; \
122 alu_overflow_val = ((int64_t)alu_val) >> 32; \
125 #if (WITH_TARGET_WORD_BITSIZE == 32)
126 #define ALU_SET(val) \
129 alu_carry_val = (uint32_t)(alu_val); \
130 alu_overflow_val = (int32_t)(alu_val); \
134 #if (WITH_TARGET_WORD_BITSIZE == 64)
135 #define ALU_ADD(val) \
137 uint64_t alu_lo = (ALU_UNSIGNED64(alu_val) \
138 + ALU_UNSIGNED64(val)); \
139 signed alu_carry = ((alu_lo & BIT(31)) != 0); \
140 alu_carry_val = (alu_carry_val \
141 + ALU_UNSIGNED64(EXTRACTED(val, 0, 31)) \
143 alu_overflow_val = (alu_overflow_val \
144 + ALU_SIGNED64(EXTRACTED(val, 0, 31)) \
146 alu_val = alu_val + val; \
149 #if (WITH_TARGET_WORD_BITSIZE == 32)
150 #define ALU_ADD(val) \
153 alu_carry_val += (uint32_t)(val); \
154 alu_overflow_val += (int32_t)(val); \
159 #if (WITH_TARGET_WORD_BITSIZE == 64)
162 signed carry = MASKED32(XER, xer_carry_bit, xer_carry_bit) != 0; \
166 #if (WITH_TARGET_WORD_BITSIZE == 32)
169 signed carry = MASKED32(XER, xer_carry_bit, xer_carry_bit) != 0; \
176 #if (WITH_TARGET_WORD_BITSIZE == 64)
178 #if (WITH_TARGET_WORD_BITSIZE == 32)
179 #define ALU_SUB(val) \
182 alu_carry_val -= (uint32_t)(val); \
183 alu_overflow_val -= (int32_t)(val); \
188 #if (WITH_TARGET_WORD_BITSIZE == 64)
190 #if (WITH_TARGET_WORD_BITSIZE == 32)
191 #define ALU_OR(val) \
194 alu_carry_val = (uint32_t)(alu_val); \
195 alu_overflow_val = (int32_t)(alu_val); \
200 #if (WITH_TARGET_WORD_BITSIZE == 64)
202 #if (WITH_TARGET_WORD_BITSIZE == 32)
203 #define ALU_XOR(val) \
206 alu_carry_val = (uint32_t)(alu_val); \
207 alu_overflow_val = (int32_t)(alu_val); \
213 #if (WITH_TARGET_WORD_BITSIZE == 64)
215 #if (WITH_TARGET_WORD_BITSIZE == 32)
218 alu_val = -alu_val; \
219 alu_carry_val = -alu_carry_val; \
220 alu_overflow_val = -alu_overflow_val; \
226 #if (WITH_TARGET_WORD_BITSIZE == 64)
228 #if (WITH_TARGET_WORD_BITSIZE == 32)
229 #define ALU_AND(val) \
232 alu_carry_val = (uint32_t)(alu_val); \
233 alu_overflow_val = (int32_t)(alu_val); \
238 #if (WITH_TARGET_WORD_BITSIZE == 64)
241 int64_t new_alu_val = ~alu_val; \
242 ALU_SET(new_alu_val); \
245 #if (WITH_TARGET_WORD_BITSIZE == 32)
248 signed new_alu_val = ~alu_val; \
249 ALU_SET(new_alu_val); \
254 /* Macros for updating the condition register */
256 #define CR1_UPDATE(Rc) \
259 CR_SET(1, EXTRACTED32(FPSCR, fpscr_fx_bit, fpscr_ox_bit)); \
264 #define _DO_CR_COMPARE(LHS, RHS) \
271 #define CR_SET(REG, VAL) MBLIT32(CR, REG*4, REG*4+3, VAL)
272 #define CR_FIELD(REG) EXTRACTED32(CR, REG*4, REG*4+3)
273 #define CR_SET_XER_SO(REG, VAL) \
275 creg new_bits = ((XER & xer_summary_overflow) \
276 ? (cr_i_summary_overflow | VAL) \
278 CR_SET(REG, new_bits); \
281 #define CR_COMPARE(REG, LHS, RHS) \
283 creg new_bits = ((XER & xer_summary_overflow) \
284 ? (cr_i_summary_overflow | _DO_CR_COMPARE(LHS,RHS)) \
285 : _DO_CR_COMPARE(LHS,RHS)); \
286 CR_SET(REG, new_bits); \
289 #define CR0_COMPARE(LHS, RHS, Rc) \
292 CR_COMPARE(0, LHS, RHS); \
294 ("CR=0x%08lx, LHS=%ld, RHS=%ld\n", \
295 (unsigned long)CR, (long)LHS, (long)RHS)); \
301 /* Bring data in from the cold */
303 #define MEM(SIGN, EA, NR_BYTES) \
304 ((SIGN##_##NR_BYTES) vm_data_map_read_##NR_BYTES(cpu_data_map(processor), EA, \
307 #define STORE(EA, NR_BYTES, VAL) \
309 vm_data_map_write_##NR_BYTES(cpu_data_map(processor), EA, VAL, \
315 /* some FPSCR update macros. */
317 #define FPSCR_BEGIN \
319 fpscreg old_fpscr ATTRIBUTE_UNUSED = FPSCR
321 #define FPSCR_END(Rc) { \
322 /* always update VX */ \
323 if ((FPSCR & fpscr_vx_bits)) \
326 FPSCR &= ~fpscr_vx; \
327 /* always update FEX */ \
328 if (((FPSCR & fpscr_vx) && (FPSCR & fpscr_ve)) \
329 || ((FPSCR & fpscr_ox) && (FPSCR & fpscr_oe)) \
330 || ((FPSCR & fpscr_ux) && (FPSCR & fpscr_ue)) \
331 || ((FPSCR & fpscr_zx) && (FPSCR & fpscr_ze)) \
332 || ((FPSCR & fpscr_xx) && (FPSCR & fpscr_xe))) \
333 FPSCR |= fpscr_fex; \
335 FPSCR &= ~fpscr_fex; \
337 /* interrupt enabled? */ \
338 if ((MSR & (msr_floating_point_exception_mode_0 \
339 | msr_floating_point_exception_mode_1)) \
340 && (FPSCR & fpscr_fex)) \
341 program_interrupt(processor, cia, \
342 floating_point_enabled_program_interrupt); \
345 #define FPSCR_SET(REG, VAL) MBLIT32(FPSCR, REG*4, REG*4+3, VAL)
346 #define FPSCR_FIELD(REG) EXTRACTED32(FPSCR, REG*4, REG*4+3)
348 #define FPSCR_SET_FPCC(VAL) MBLIT32(FPSCR, fpscr_fpcc_bit, fpscr_fpcc_bit+3, VAL)
350 /* Handle various exceptions */
352 #define FPSCR_OR_VX(VAL) \
354 /* NOTE: VAL != 0 */ \
359 #define FPSCR_SET_OX(COND) \
366 FPSCR &= ~fpscr_ox; \
369 #define FPSCR_SET_UX(COND) \
376 FPSCR &= ~fpscr_ux; \
379 #define FPSCR_SET_ZX(COND) \
386 FPSCR &= ~fpscr_zx; \
389 #define FPSCR_SET_XX(COND) \
397 /* Note: code using SET_FI must also explicitly call SET_XX */
399 #define FPSCR_SET_FR(COND) do { \
403 FPSCR &= ~fpscr_fr; \
406 #define FPSCR_SET_FI(COND) \
412 FPSCR &= ~fpscr_fi; \
415 #define FPSCR_SET_FPRF(VAL) \
417 FPSCR = (FPSCR & ~fpscr_fprf) | (VAL); \