1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, 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 & BIT32(0))) \
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_SET_XER_SO(REG, VAL) \
266 creg new_bits = ((XER & xer_summary_overflow) \
267 ? (cr_i_summary_overflow | VAL) \
269 CR_SET(REG, new_bits); \
272 #define CR_COMPARE(REG, LHS, RHS) \
274 creg new_bits = ((XER & xer_summary_overflow) \
275 ? (cr_i_summary_overflow | _DO_CR_COMPARE(LHS,RHS)) \
276 : _DO_CR_COMPARE(LHS,RHS)); \
277 CR_SET(REG, new_bits); \
280 #define CR0_COMPARE(LHS, RHS, Rc) \
283 CR_COMPARE(0, LHS, RHS); \
285 ("CR=0x%08lx, LHS=%ld, RHS=%ld\n", \
286 (unsigned long)CR, (long)LHS, (long)RHS)); \
292 /* Bring data in from the cold */
294 #define MEM(SIGN, EA, NR_BYTES) \
295 ((SIGN##_##NR_BYTES) vm_data_map_read_##NR_BYTES(cpu_data_map(processor), EA, \
298 #define STORE(EA, NR_BYTES, VAL) \
300 vm_data_map_write_##NR_BYTES(cpu_data_map(processor), EA, VAL, \
305 /* some FPSCR update macros */
307 #define FPSCR_BEGIN \
308 FPSCR &= ~fpscr_reserved_20; \
310 fpscreg old_fpscr __attribute__((__unused__)) = FPSCR
312 #define FPSCR_END(Rc) { \
314 if (FPSCR & fpscr_reserved_20) { \
315 FPSCR &= ~fpscr_reserved_20; \
316 program_interrupt(processor, cia, \
317 floating_point_enabled_program_interrupt); \
321 #define FPSCR_SET_FPCC(VAL) MBLIT32(FPSCR, fpscr_fpcc_bit, fpscr_fpcc_bit+3, VAL)
323 /* Handle various exceptions */
325 #define FPSCR_OR_VX(VAL) \
329 if (FPSCR & fpscr_ve) \
330 FPSCR |= fpscr_fex | fpscr_reserved_20; \
334 #define FPSCR_SET_OX(COND) \
339 if (FPSCR & fpscr_oe) \
340 FPSCR |= fpscr_fex | fpscr_reserved_20; \
343 FPSCR &= ~fpscr_ox; \
346 #define FPSCR_SET_UX(COND) \
351 if (FPSCR & fpscr_ue) \
352 FPSCR |= fpscr_fex | fpscr_reserved_20; \
355 FPSCR &= ~fpscr_ux; \
358 #define FPSCR_SET_ZX(COND) \
363 if (FPSCR & fpscr_ze) \
364 FPSCR |= fpscr_fex | fpscr_reserved_20; \
367 FPSCR &= ~fpscr_zx; \
370 #define FPSCR_SET_XX(COND) \
375 if (FPSCR & fpscr_xe) \
376 FPSCR |= fpscr_fex | fpscr_reserved_20; \
380 #define FPSCR_SET_FR(COND) \
385 FPSCR &= ~fpscr_fr; \
388 #define FPSCR_SET_FI(COND) \
393 FPSCR &= ~fpscr_fi; \
396 #define FPSCR_SET_FPRF(VAL) \
398 FPSCR = (FPSCR & ~fpscr_fprf) | (VAL); \