1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
4 Copyright (C) 1997, Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 /* 32bit target expressions:
30 Each calculation is performed three times using each of the
31 signed64, unsigned64 and long integer types. The macro ALU_END
32 (in _ALU_RESULT_VAL) then selects which of the three alternative
33 results will be used in the final assignment of the target
34 register. As this selection is determined at compile time by
35 fields in the instruction (OE, EA, Rc) the compiler has sufficient
36 information to firstly simplify the selection code into a single
37 case and then back anotate the equations and hence eliminate any
38 resulting dead code. That dead code being the calculations that,
39 as it turned out were not in the end needed.
41 64bit arrithemetic is used firstly because it allows the use of
42 gcc's efficient long long operators (typically efficiently output
43 inline) and secondly because the resultant answer will contain in
44 the low 32bits the answer while in the high 32bits is either carry
45 or status information. */
49 /* 16bit target expressions:
51 These are a simplified version of the 32bit target expressions */
55 /* 64bit target expressions:
57 Unfortunatly 128bit arrithemetic isn't that common. Consequently
58 the 32/64 bit trick can not be used. Instead all calculations are
59 required to retain carry/overflow information in separate
60 variables. Even with this restriction it is still possible for the
61 trick of letting the compiler discard the calculation of unneeded
65 /* Start a section of ALU code */
67 #define ALU16_BEGIN(VAL) \
69 signed_word alu_carry_val; \
70 unsigned_word alu_overflow_val; \
73 #define ALU32_BEGIN(VAL) \
75 natural_word alu_val; \
76 unsigned64 alu_carry_val; \
77 signed64 alu_overflow_val; \
80 #define ALU64_BEGIN(VAL) \
83 unsigned64 alu_carry_val; \
84 signed64 alu_overflow_val; \
88 #define ALU_BEGIN(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_BEGIN)(VAL)
90 /* More basic alu operations */
94 #define ALU16_SET(VAL) \
96 alu_carry_val = (unsigned16)(VAL); \
97 alu_overflow_val = (signed16)(VAL); \
100 #define ALU32_SET(VAL) \
102 alu_val = (unsigned32)(VAL); \
103 alu_carry_val = (unsigned32)(alu_val); \
104 alu_overflow_val = (signed32)(alu_val); \
107 #define ALU64_SET(VAL) \
110 alu_carry_val = ((unsigned64)alu_val) >> 32; \
111 alu_overflow_val = ((signed64)alu_val) >> 32; \
114 #define ALU_SET(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_SET)(VAL)
119 #define ALU16_ADD(VAL) \
121 alu_carry_val += (unsigned16)(VAL); \
122 alu_overflow_val += (signed16)(VAL); \
125 #define ALU32_ADD(VAL) \
128 alu_carry_val += (unsigned32)(VAL); \
129 alu_overflow_val += (signed32)(VAL); \
132 #define ALU64_ADD(VAL) \
134 unsigned64 val = (VAL); \
135 unsigned64 alu_lo = alu_val + val; \
136 signed alu_carry = ((alu_lo & LSBIT64 (31)) != 0); \
137 alu_carry_val = (alu_carry_val \
138 + MSEXTRACTED64 (val, 0, 31) \
140 alu_overflow_val = (alu_overflow_val \
141 + MSEXTRACTED64 (val, 0, 31) \
143 alu_val = alu_val + val; \
146 #define ALU_ADD(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_ADD)(VAL)
151 #define ALU16_ADD_CA \
153 signed carry = ALU_CARRY; \
157 #define ALU32_ADD_CA \
159 signed carry = ALU_CARRY; \
163 #define ALU64_ADD_CA \
165 signed carry = ALU_CARRY; \
169 #define ALU_ADD_CA XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_ADD_CA)
174 #define ALU16_SUB(VAL) \
176 alu_carry_val -= (unsigned16)(VAL); \
177 alu_overflow_val -= (signed16)(VAL); \
180 #define ALU32_SUB(VAL) \
183 alu_carry_val -= (unsigned32)(VAL); \
184 alu_overflow_val -= (signed32)(VAL); \
187 #define ALU64_SUB(VAL) \
189 signed64 subval = -(VAL); /* -MININT? */ \
190 ALU64_ADD (subval); \
193 #define ALU_SUB(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_SUB)(VAL)
198 #define ALU16_SUB_CA \
200 signed carry = ALU_CARRY; \
204 #define ALU32_SUB_CA \
206 signed carry = ALU_CARRY; \
210 #define ALU64_SUB_CA \
212 signed carry = ALU_CARRY; \
216 #define ALU_SUB_CA XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_SUB_CA)
221 #define ALU16_OR(VAL) \
226 #define ALU32_OR(VAL) \
229 alu_carry_val = (unsigned32)(alu_val); \
230 alu_overflow_val = (signed32)(alu_val); \
233 #define ALU64_OR(VAL) \
238 #define ALU_OR(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_OR)(VAL)
243 #define ALU16_XOR(VAL) \
245 error("ALU16_XOR"); \
248 #define ALU32_XOR(VAL) \
251 alu_carry_val = (unsigned32)(alu_val); \
252 alu_overflow_val = (signed32)(alu_val); \
255 #define ALU64_XOR(VAL) \
257 error("ALU_XOR64"); \
260 #define ALU_XOR(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_XOR)(VAL)
265 #define ALU16_NEGATE \
267 error("ALU_NEGATE16"); \
270 #define ALU32_NEGATE \
272 alu_val = -alu_val; \
273 alu_carry_val = -alu_carry_val; \
274 alu_overflow_val = -alu_overflow_val; \
277 #define ALU64_NEGATE \
279 error("ALU_NEGATE64"); \
282 #define ALU_NEGATE XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_NEGATE)
287 #define ALU16_AND(VAL) \
289 error("ALU_AND16"); \
292 #define ALU32_AND(VAL) \
295 alu_carry_val = (unsigned32)(alu_val); \
296 alu_overflow_val = (signed32)(alu_val); \
299 #define ALU64_AND(VAL) \
301 error("ALU_AND64"); \
304 #define ALU_AND(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_AND)(VAL)
309 #define ALU16_NOT(VAL) \
311 error("ALU_NOT16"); \
316 signed64 new_alu_val = ~alu_val; \
317 ALU_SET(new_alu_val); \
322 error("ALU_NOT64"); \
325 #define ALU_NOT XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_NOT)
329 /* Make available various results */
332 /* overflow occures if the sign bit differs from the carry bit */
334 #define ALU16_HAD_OVERFLOW \
335 (!(alu_overflow_val & MSBIT32 (0)) != !(alu_overflow_val & MSBIT32 (16)))
337 #define ALU32_HAD_OVERFLOW \
338 ((((unsigned64)(alu_overflow_val & MSBIT64(0))) >> 32) \
339 != (unsigned64)(alu_overflow_val & MSBIT64(32)))
341 #define ALU64_HAD_OVERFLOW \
342 ((alu_val & MSBIT64 (0)) != (alu_overflow_val & MSBIT64 (0)))
344 #define ALU_HAD_OVERFLOW XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_HAD_OVERFLOW)
347 /* carry found in bit before sign */
349 #define ALU16_HAD_CARRY \
350 (alu_carry_val & MSBIT32(16))
352 #define ALU32_HAD_CARRY \
353 (alu_carry_val & MSBIT64(31))