1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Einfo
; use Einfo
;
29 with Exp_Util
; use Exp_Util
;
30 with Nlists
; use Nlists
;
31 with Nmake
; use Nmake
;
32 with Restrict
; use Restrict
;
33 with Rident
; use Rident
;
34 with Rtsfind
; use Rtsfind
;
36 with Sem_Eval
; use Sem_Eval
;
37 with Sem_Res
; use Sem_Res
;
38 with Sem_Util
; use Sem_Util
;
39 with Sinfo
; use Sinfo
;
40 with Snames
; use Snames
;
41 with Stand
; use Stand
;
42 with Tbuild
; use Tbuild
;
43 with Uintp
; use Uintp
;
44 with Urealp
; use Urealp
;
46 package body Exp_Fixd
is
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
52 -- General note; in this unit, a number of routines are driven by the
53 -- types (Etype) of their operands. Since we are dealing with unanalyzed
54 -- expressions as they are constructed, the Etypes would not normally be
55 -- set, but the construction routines that we use in this unit do in fact
56 -- set the Etype values correctly. In addition, setting the Etype ensures
57 -- that the analyzer does not try to redetermine the type when the node
58 -- is analyzed (which would be wrong, since in the case where we set the
59 -- Treat_Fixed_As_Integer or Conversion_OK flags, it would think it was
60 -- still dealing with a normal fixed-point operation and mess it up).
62 function Build_Conversion
66 Rchk
: Boolean := False;
67 Trunc
: Boolean := False) return Node_Id
;
68 -- Build an expression that converts the expression Expr to type Typ,
69 -- taking the source location from Sloc (N). If the conversions involve
70 -- fixed-point types, then the Conversion_OK flag will be set so that the
71 -- resulting conversions do not get re-expanded. On return the resulting
72 -- node has its Etype set. If Rchk is set, then Do_Range_Check is set
73 -- in the resulting conversion node. If Trunc is set, then the
74 -- Float_Truncate flag is set on the conversion, which must be from
75 -- a floating-point type to an integer type.
77 function Build_Divide
(N
: Node_Id
; L
, R
: Node_Id
) return Node_Id
;
78 -- Builds an N_Op_Divide node from the given left and right operand
79 -- expressions, using the source location from Sloc (N). The operands are
80 -- either both Universal_Real, in which case Build_Divide differs from
81 -- Make_Op_Divide only in that the Etype of the resulting node is set (to
82 -- Universal_Real), or they can be integer types. In this case the integer
83 -- types need not be the same, and Build_Divide converts the operand with
84 -- the smaller sized type to match the type of the other operand and sets
85 -- this as the result type. The Rounded_Result flag of the result in this
86 -- case is set from the Rounded_Result flag of node N. On return, the
87 -- resulting node is analyzed, and has its Etype set.
89 function Build_Double_Divide
91 X
, Y
, Z
: Node_Id
) return Node_Id
;
92 -- Returns a node corresponding to the value X/(Y*Z) using the source
93 -- location from Sloc (N). The division is rounded if the Rounded_Result
94 -- flag of N is set. The integer types of X, Y, Z may be different. On
95 -- return the resulting node is analyzed, and has its Etype set.
97 procedure Build_Double_Divide_Code
100 Qnn
, Rnn
: out Entity_Id
;
102 -- Generates a sequence of code for determining the quotient and remainder
103 -- of the division X/(Y*Z), using the source location from Sloc (N).
104 -- Entities of appropriate types are allocated for the quotient and
105 -- remainder and returned in Qnn and Rnn. The result is rounded if the
106 -- Rounded_Result flag of N is set. The Etype fields of Qnn and Rnn are
107 -- appropriately set on return.
109 function Build_Multiply
(N
: Node_Id
; L
, R
: Node_Id
) return Node_Id
;
110 -- Builds an N_Op_Multiply node from the given left and right operand
111 -- expressions, using the source location from Sloc (N). The operands are
112 -- either both Universal_Real, in which case Build_Multiply differs from
113 -- Make_Op_Multiply only in that the Etype of the resulting node is set (to
114 -- Universal_Real), or they can be integer types. In this case the integer
115 -- types need not be the same, and Build_Multiply chooses a type long
116 -- enough to hold the product (i.e. twice the size of the longer of the two
117 -- operand types), and both operands are converted to this type. The Etype
118 -- of the result is also set to this value. However, the result can never
119 -- overflow Integer_64, so this is the largest type that is ever generated.
120 -- On return, the resulting node is analyzed and has its Etype set.
122 function Build_Rem
(N
: Node_Id
; L
, R
: Node_Id
) return Node_Id
;
123 -- Builds an N_Op_Rem node from the given left and right operand
124 -- expressions, using the source location from Sloc (N). The operands are
125 -- both integer types, which need not be the same. Build_Rem converts the
126 -- operand with the smaller sized type to match the type of the other
127 -- operand and sets this as the result type. The result is never rounded
128 -- (rem operations cannot be rounded in any case). On return, the resulting
129 -- node is analyzed and has its Etype set.
131 function Build_Scaled_Divide
133 X
, Y
, Z
: Node_Id
) return Node_Id
;
134 -- Returns a node corresponding to the value X*Y/Z using the source
135 -- location from Sloc (N). The division is rounded if the Rounded_Result
136 -- flag of N is set. The integer types of X, Y, Z may be different. On
137 -- return the resulting node is analyzed and has is Etype set.
139 procedure Build_Scaled_Divide_Code
142 Qnn
, Rnn
: out Entity_Id
;
144 -- Generates a sequence of code for determining the quotient and remainder
145 -- of the division X*Y/Z, using the source location from Sloc (N). Entities
146 -- of appropriate types are allocated for the quotient and remainder and
147 -- returned in Qnn and Rrr. The integer types for X, Y, Z may be different.
148 -- The division is rounded if the Rounded_Result flag of N is set. The
149 -- Etype fields of Qnn and Rnn are appropriately set on return.
151 procedure Do_Divide_Fixed_Fixed
(N
: Node_Id
);
152 -- Handles expansion of divide for case of two fixed-point operands
153 -- (neither of them universal), with an integer or fixed-point result.
154 -- N is the N_Op_Divide node to be expanded.
156 procedure Do_Divide_Fixed_Universal
(N
: Node_Id
);
157 -- Handles expansion of divide for case of a fixed-point operand divided
158 -- by a universal real operand, with an integer or fixed-point result. N
159 -- is the N_Op_Divide node to be expanded.
161 procedure Do_Divide_Universal_Fixed
(N
: Node_Id
);
162 -- Handles expansion of divide for case of a universal real operand
163 -- divided by a fixed-point operand, with an integer or fixed-point
164 -- result. N is the N_Op_Divide node to be expanded.
166 procedure Do_Multiply_Fixed_Fixed
(N
: Node_Id
);
167 -- Handles expansion of multiply for case of two fixed-point operands
168 -- (neither of them universal), with an integer or fixed-point result.
169 -- N is the N_Op_Multiply node to be expanded.
171 procedure Do_Multiply_Fixed_Universal
(N
: Node_Id
; Left
, Right
: Node_Id
);
172 -- Handles expansion of multiply for case of a fixed-point operand
173 -- multiplied by a universal real operand, with an integer or fixed-
174 -- point result. N is the N_Op_Multiply node to be expanded, and
175 -- Left, Right are the operands (which may have been switched).
177 procedure Expand_Convert_Fixed_Static
(N
: Node_Id
);
178 -- This routine is called where the node N is a conversion of a literal
179 -- or other static expression of a fixed-point type to some other type.
180 -- In such cases, we simply rewrite the operand as a real literal and
181 -- reanalyze. This avoids problems which would otherwise result from
182 -- attempting to build and fold expressions involving constants.
184 function Fpt_Value
(N
: Node_Id
) return Node_Id
;
185 -- Given an operand of fixed-point operation, return an expression that
186 -- represents the corresponding Universal_Real value. The expression
187 -- can be of integer type, floating-point type, or fixed-point type.
188 -- The expression returned is neither analyzed and resolved. The Etype
189 -- of the result is properly set (to Universal_Real).
191 function Integer_Literal
194 Negative
: Boolean := False) return Node_Id
;
195 -- Given a non-negative universal integer value, build a typed integer
196 -- literal node, using the smallest applicable standard integer type. If
197 -- and only if Negative is true a negative literal is built. If V exceeds
198 -- 2**63-1, the largest value allowed for perfect result set scaling
199 -- factors (see RM G.2.3(22)), then Empty is returned. The node N provides
200 -- the Sloc value for the constructed literal. The Etype of the resulting
201 -- literal is correctly set, and it is marked as analyzed.
203 function Real_Literal
(N
: Node_Id
; V
: Ureal
) return Node_Id
;
204 -- Build a real literal node from the given value, the Etype of the
205 -- returned node is set to Universal_Real, since all floating-point
206 -- arithmetic operations that we construct use Universal_Real
208 function Rounded_Result_Set
(N
: Node_Id
) return Boolean;
209 -- Returns True if N is a node that contains the Rounded_Result flag
210 -- and if the flag is true or the target type is an integer type.
215 Rchk
: Boolean := False;
216 Trunc
: Boolean := False);
217 -- N is the node for the current conversion, division or multiplication
218 -- operation, and Expr is an expression representing the result. Expr may
219 -- be of floating-point or integer type. If the operation result is fixed-
220 -- point, then the value of Expr is in units of small of the result type
221 -- (i.e. small's have already been dealt with). The result of the call is
222 -- to replace N by an appropriate conversion to the result type, dealing
223 -- with rounding for the decimal types case. The node is then analyzed and
224 -- resolved using the result type. If Rchk or Trunc are True, then
225 -- respectively Do_Range_Check and Float_Truncate are set in the
226 -- resulting conversion.
228 ----------------------
229 -- Build_Conversion --
230 ----------------------
232 function Build_Conversion
236 Rchk
: Boolean := False;
237 Trunc
: Boolean := False) return Node_Id
239 Loc
: constant Source_Ptr
:= Sloc
(N
);
241 Rcheck
: Boolean := Rchk
;
244 -- A special case, if the expression is an integer literal and the
245 -- target type is an integer type, then just retype the integer
246 -- literal to the desired target type. Don't do this if we need
249 if Nkind
(Expr
) = N_Integer_Literal
250 and then Is_Integer_Type
(Typ
)
255 -- Cases where we end up with a conversion. Note that we do not use the
256 -- Convert_To abstraction here, since we may be decorating the resulting
257 -- conversion with Rounded_Result and/or Conversion_OK, so we want the
258 -- conversion node present, even if it appears to be redundant.
261 -- Remove inner conversion if both inner and outer conversions are
262 -- to integer types, since the inner one serves no purpose (except
263 -- perhaps to set rounding, so we preserve the Rounded_Result flag)
264 -- and also we preserve the range check flag on the inner operand
266 if Is_Integer_Type
(Typ
)
267 and then Is_Integer_Type
(Etype
(Expr
))
268 and then Nkind
(Expr
) = N_Type_Conversion
271 Make_Type_Conversion
(Loc
,
272 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
273 Expression
=> Expression
(Expr
));
274 Set_Rounded_Result
(Result
, Rounded_Result_Set
(Expr
));
275 Rcheck
:= Rcheck
or Do_Range_Check
(Expr
);
277 -- For all other cases, a simple type conversion will work
281 Make_Type_Conversion
(Loc
,
282 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
285 Set_Float_Truncate
(Result
, Trunc
);
288 -- Set Conversion_OK if either result or expression type is a
289 -- fixed-point type, since from a semantic point of view, we are
290 -- treating fixed-point values as integers at this stage.
292 if Is_Fixed_Point_Type
(Typ
)
293 or else Is_Fixed_Point_Type
(Etype
(Expression
(Result
)))
295 Set_Conversion_OK
(Result
);
298 -- Set Do_Range_Check if either it was requested by the caller,
299 -- or if an eliminated inner conversion had a range check.
302 Enable_Range_Check
(Result
);
304 Set_Do_Range_Check
(Result
, False);
308 Set_Etype
(Result
, Typ
);
310 end Build_Conversion
;
316 function Build_Divide
(N
: Node_Id
; L
, R
: Node_Id
) return Node_Id
is
317 Loc
: constant Source_Ptr
:= Sloc
(N
);
318 Left_Type
: constant Entity_Id
:= Base_Type
(Etype
(L
));
319 Right_Type
: constant Entity_Id
:= Base_Type
(Etype
(R
));
320 Result_Type
: Entity_Id
;
324 -- Deal with floating-point case first
326 if Is_Floating_Point_Type
(Left_Type
) then
327 pragma Assert
(Left_Type
= Universal_Real
);
328 pragma Assert
(Right_Type
= Universal_Real
);
330 Rnode
:= Make_Op_Divide
(Loc
, L
, R
);
331 Result_Type
:= Universal_Real
;
333 -- Integer and fixed-point cases
336 -- An optimization. If the right operand is the literal 1, then we
337 -- can just return the left hand operand. Putting the optimization
338 -- here allows us to omit the check at the call site.
340 if Nkind
(R
) = N_Integer_Literal
and then Intval
(R
) = 1 then
344 -- If left and right types are the same, no conversion needed
346 if Left_Type
= Right_Type
then
347 Result_Type
:= Left_Type
;
353 -- Use left type if it is the larger of the two
355 elsif Esize
(Left_Type
) >= Esize
(Right_Type
) then
356 Result_Type
:= Left_Type
;
360 Right_Opnd
=> Build_Conversion
(N
, Left_Type
, R
));
362 -- Otherwise right type is larger of the two, us it
365 Result_Type
:= Right_Type
;
368 Left_Opnd
=> Build_Conversion
(N
, Right_Type
, L
),
373 -- We now have a divide node built with Result_Type set. First
374 -- set Etype of result, as required for all Build_xxx routines
376 Set_Etype
(Rnode
, Base_Type
(Result_Type
));
378 -- Set Treat_Fixed_As_Integer if operation on fixed-point type
379 -- since this is a literal arithmetic operation, to be performed
380 -- by Gigi without any consideration of small values.
382 if Is_Fixed_Point_Type
(Result_Type
) then
383 Set_Treat_Fixed_As_Integer
(Rnode
);
386 -- The result is rounded if the target of the operation is decimal
387 -- and Rounded_Result is set, or if the target of the operation
388 -- is an integer type.
390 if Is_Integer_Type
(Etype
(N
))
391 or else Rounded_Result_Set
(N
)
393 Set_Rounded_Result
(Rnode
);
399 -------------------------
400 -- Build_Double_Divide --
401 -------------------------
403 function Build_Double_Divide
405 X
, Y
, Z
: Node_Id
) return Node_Id
407 Y_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Y
)));
408 Z_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Z
)));
412 -- If denominator fits in 64 bits, we can build the operations directly
413 -- without causing any intermediate overflow, so that's what we do.
415 if Int
'Max (Y_Size
, Z_Size
) <= 32 then
417 Build_Divide
(N
, X
, Build_Multiply
(N
, Y
, Z
));
419 -- Otherwise we use the runtime routine
421 -- [Qnn : Interfaces.Integer_64,
422 -- Rnn : Interfaces.Integer_64;
423 -- Double_Divide (X, Y, Z, Qnn, Rnn, Round);
428 Loc
: constant Source_Ptr
:= Sloc
(N
);
433 pragma Warnings
(Off
, Rnn
);
436 Build_Double_Divide_Code
(N
, X
, Y
, Z
, Qnn
, Rnn
, Code
);
437 Insert_Actions
(N
, Code
);
438 Expr
:= New_Occurrence_Of
(Qnn
, Loc
);
440 -- Set type of result in case used elsewhere (see note at start)
442 Set_Etype
(Expr
, Etype
(Qnn
));
444 -- Set result as analyzed (see note at start on build routines)
449 end Build_Double_Divide
;
451 ------------------------------
452 -- Build_Double_Divide_Code --
453 ------------------------------
455 -- If the denominator can be computed in 64-bits, we build
457 -- [Nnn : constant typ := typ (X);
458 -- Dnn : constant typ := typ (Y) * typ (Z)
459 -- Qnn : constant typ := Nnn / Dnn;
460 -- Rnn : constant typ := Nnn / Dnn;
462 -- If the numerator cannot be computed in 64 bits, we build
466 -- Double_Divide (X, Y, Z, Qnn, Rnn, Round);]
468 procedure Build_Double_Divide_Code
471 Qnn
, Rnn
: out Entity_Id
;
474 Loc
: constant Source_Ptr
:= Sloc
(N
);
476 X_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(X
)));
477 Y_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Y
)));
478 Z_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Z
)));
490 -- Find type that will allow computation of numerator
492 QR_Siz
:= Int
'Max (X_Size
, 2 * Int
'Max (Y_Size
, Z_Size
));
495 QR_Typ
:= Standard_Integer_16
;
496 elsif QR_Siz
<= 32 then
497 QR_Typ
:= Standard_Integer_32
;
498 elsif QR_Siz
<= 64 then
499 QR_Typ
:= Standard_Integer_64
;
501 -- For more than 64, bits, we use the 64-bit integer defined in
502 -- Interfaces, so that it can be handled by the runtime routine
505 QR_Typ
:= RTE
(RE_Integer_64
);
508 -- Define quotient and remainder, and set their Etypes, so
509 -- that they can be picked up by Build_xxx routines.
511 Qnn
:= Make_Temporary
(Loc
, 'S');
512 Rnn
:= Make_Temporary
(Loc
, 'R');
514 Set_Etype
(Qnn
, QR_Typ
);
515 Set_Etype
(Rnn
, QR_Typ
);
517 -- Case that we can compute the denominator in 64 bits
521 -- Create temporaries for numerator and denominator and set Etypes,
522 -- so that New_Occurrence_Of picks them up for Build_xxx calls.
524 Nnn
:= Make_Temporary
(Loc
, 'N');
525 Dnn
:= Make_Temporary
(Loc
, 'D');
527 Set_Etype
(Nnn
, QR_Typ
);
528 Set_Etype
(Dnn
, QR_Typ
);
531 Make_Object_Declaration
(Loc
,
532 Defining_Identifier
=> Nnn
,
533 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
534 Constant_Present
=> True,
535 Expression
=> Build_Conversion
(N
, QR_Typ
, X
)),
537 Make_Object_Declaration
(Loc
,
538 Defining_Identifier
=> Dnn
,
539 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
540 Constant_Present
=> True,
543 Build_Conversion
(N
, QR_Typ
, Y
),
544 Build_Conversion
(N
, QR_Typ
, Z
))));
548 New_Occurrence_Of
(Nnn
, Loc
),
549 New_Occurrence_Of
(Dnn
, Loc
));
551 Set_Rounded_Result
(Quo
, Rounded_Result_Set
(N
));
554 Make_Object_Declaration
(Loc
,
555 Defining_Identifier
=> Qnn
,
556 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
557 Constant_Present
=> True,
561 Make_Object_Declaration
(Loc
,
562 Defining_Identifier
=> Rnn
,
563 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
564 Constant_Present
=> True,
567 New_Occurrence_Of
(Nnn
, Loc
),
568 New_Occurrence_Of
(Dnn
, Loc
))));
570 -- Case where denominator does not fit in 64 bits, so we have to
571 -- call the runtime routine to compute the quotient and remainder
574 Rnd
:= Boolean_Literals
(Rounded_Result_Set
(N
));
577 Make_Object_Declaration
(Loc
,
578 Defining_Identifier
=> Qnn
,
579 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
)),
581 Make_Object_Declaration
(Loc
,
582 Defining_Identifier
=> Rnn
,
583 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
)),
585 Make_Procedure_Call_Statement
(Loc
,
586 Name
=> New_Occurrence_Of
(RTE
(RE_Double_Divide
), Loc
),
587 Parameter_Associations
=> New_List
(
588 Build_Conversion
(N
, QR_Typ
, X
),
589 Build_Conversion
(N
, QR_Typ
, Y
),
590 Build_Conversion
(N
, QR_Typ
, Z
),
591 New_Occurrence_Of
(Qnn
, Loc
),
592 New_Occurrence_Of
(Rnn
, Loc
),
593 New_Occurrence_Of
(Rnd
, Loc
))));
595 end Build_Double_Divide_Code
;
601 function Build_Multiply
(N
: Node_Id
; L
, R
: Node_Id
) return Node_Id
is
602 Loc
: constant Source_Ptr
:= Sloc
(N
);
603 Left_Type
: constant Entity_Id
:= Etype
(L
);
604 Right_Type
: constant Entity_Id
:= Etype
(R
);
608 Result_Type
: Entity_Id
;
612 -- Deal with floating-point case first
614 if Is_Floating_Point_Type
(Left_Type
) then
615 pragma Assert
(Left_Type
= Universal_Real
);
616 pragma Assert
(Right_Type
= Universal_Real
);
618 Result_Type
:= Universal_Real
;
619 Rnode
:= Make_Op_Multiply
(Loc
, L
, R
);
621 -- Integer and fixed-point cases
624 -- An optimization. If the right operand is the literal 1, then we
625 -- can just return the left hand operand. Putting the optimization
626 -- here allows us to omit the check at the call site. Similarly, if
627 -- the left operand is the integer 1 we can return the right operand.
629 if Nkind
(R
) = N_Integer_Literal
and then Intval
(R
) = 1 then
631 elsif Nkind
(L
) = N_Integer_Literal
and then Intval
(L
) = 1 then
635 -- Otherwise we need to figure out the correct result type size
636 -- First figure out the effective sizes of the operands. Normally
637 -- the effective size of an operand is the RM_Size of the operand.
638 -- But a special case arises with operands whose size is known at
639 -- compile time. In this case, we can use the actual value of the
640 -- operand to get its size if it would fit signed in 8 or 16 bits.
642 Left_Size
:= UI_To_Int
(RM_Size
(Left_Type
));
644 if Compile_Time_Known_Value
(L
) then
646 Val
: constant Uint
:= Expr_Value
(L
);
648 if Val
< Int
'(2 ** 7) then
650 elsif Val < Int'(2 ** 15) then
656 Right_Size
:= UI_To_Int
(RM_Size
(Right_Type
));
658 if Compile_Time_Known_Value
(R
) then
660 Val
: constant Uint
:= Expr_Value
(R
);
662 if Val
<= Int
'(2 ** 7) then
664 elsif Val <= Int'(2 ** 15) then
670 -- Now the result size must be at least twice the longer of
671 -- the two sizes, to accommodate all possible results.
673 Rsize
:= 2 * Int
'Max (Left_Size
, Right_Size
);
676 Result_Type
:= Standard_Integer_8
;
678 elsif Rsize
<= 16 then
679 Result_Type
:= Standard_Integer_16
;
681 elsif Rsize
<= 32 then
682 Result_Type
:= Standard_Integer_32
;
685 Result_Type
:= Standard_Integer_64
;
689 Make_Op_Multiply
(Loc
,
690 Left_Opnd
=> Build_Conversion
(N
, Result_Type
, L
),
691 Right_Opnd
=> Build_Conversion
(N
, Result_Type
, R
));
694 -- We now have a multiply node built with Result_Type set. First
695 -- set Etype of result, as required for all Build_xxx routines
697 Set_Etype
(Rnode
, Base_Type
(Result_Type
));
699 -- Set Treat_Fixed_As_Integer if operation on fixed-point type
700 -- since this is a literal arithmetic operation, to be performed
701 -- by Gigi without any consideration of small values.
703 if Is_Fixed_Point_Type
(Result_Type
) then
704 Set_Treat_Fixed_As_Integer
(Rnode
);
714 function Build_Rem
(N
: Node_Id
; L
, R
: Node_Id
) return Node_Id
is
715 Loc
: constant Source_Ptr
:= Sloc
(N
);
716 Left_Type
: constant Entity_Id
:= Etype
(L
);
717 Right_Type
: constant Entity_Id
:= Etype
(R
);
718 Result_Type
: Entity_Id
;
722 if Left_Type
= Right_Type
then
723 Result_Type
:= Left_Type
;
729 -- If left size is larger, we do the remainder operation using the
730 -- size of the left type (i.e. the larger of the two integer types).
732 elsif Esize
(Left_Type
) >= Esize
(Right_Type
) then
733 Result_Type
:= Left_Type
;
737 Right_Opnd
=> Build_Conversion
(N
, Left_Type
, R
));
739 -- Similarly, if the right size is larger, we do the remainder
740 -- operation using the right type.
743 Result_Type
:= Right_Type
;
746 Left_Opnd
=> Build_Conversion
(N
, Right_Type
, L
),
750 -- We now have an N_Op_Rem node built with Result_Type set. First
751 -- set Etype of result, as required for all Build_xxx routines
753 Set_Etype
(Rnode
, Base_Type
(Result_Type
));
755 -- Set Treat_Fixed_As_Integer if operation on fixed-point type
756 -- since this is a literal arithmetic operation, to be performed
757 -- by Gigi without any consideration of small values.
759 if Is_Fixed_Point_Type
(Result_Type
) then
760 Set_Treat_Fixed_As_Integer
(Rnode
);
763 -- One more check. We did the rem operation using the larger of the
764 -- two types, which is reasonable. However, in the case where the
765 -- two types have unequal sizes, it is impossible for the result of
766 -- a remainder operation to be larger than the smaller of the two
767 -- types, so we can put a conversion round the result to keep the
768 -- evolving operation size as small as possible.
770 if Esize
(Left_Type
) >= Esize
(Right_Type
) then
771 Rnode
:= Build_Conversion
(N
, Right_Type
, Rnode
);
772 elsif Esize
(Right_Type
) >= Esize
(Left_Type
) then
773 Rnode
:= Build_Conversion
(N
, Left_Type
, Rnode
);
779 -------------------------
780 -- Build_Scaled_Divide --
781 -------------------------
783 function Build_Scaled_Divide
785 X
, Y
, Z
: Node_Id
) return Node_Id
787 X_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(X
)));
788 Y_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Y
)));
792 -- If numerator fits in 64 bits, we can build the operations directly
793 -- without causing any intermediate overflow, so that's what we do.
795 if Int
'Max (X_Size
, Y_Size
) <= 32 then
797 Build_Divide
(N
, Build_Multiply
(N
, X
, Y
), Z
);
799 -- Otherwise we use the runtime routine
801 -- [Qnn : Integer_64,
803 -- Scaled_Divide (X, Y, Z, Qnn, Rnn, Round);
808 Loc
: constant Source_Ptr
:= Sloc
(N
);
813 pragma Warnings
(Off
, Rnn
);
816 Build_Scaled_Divide_Code
(N
, X
, Y
, Z
, Qnn
, Rnn
, Code
);
817 Insert_Actions
(N
, Code
);
818 Expr
:= New_Occurrence_Of
(Qnn
, Loc
);
820 -- Set type of result in case used elsewhere (see note at start)
822 Set_Etype
(Expr
, Etype
(Qnn
));
826 end Build_Scaled_Divide
;
828 ------------------------------
829 -- Build_Scaled_Divide_Code --
830 ------------------------------
832 -- If the numerator can be computed in 64-bits, we build
834 -- [Nnn : constant typ := typ (X) * typ (Y);
835 -- Dnn : constant typ := typ (Z)
836 -- Qnn : constant typ := Nnn / Dnn;
837 -- Rnn : constant typ := Nnn / Dnn;
839 -- If the numerator cannot be computed in 64 bits, we build
841 -- [Qnn : Interfaces.Integer_64;
842 -- Rnn : Interfaces.Integer_64;
843 -- Scaled_Divide (X, Y, Z, Qnn, Rnn, Round);]
845 procedure Build_Scaled_Divide_Code
848 Qnn
, Rnn
: out Entity_Id
;
851 Loc
: constant Source_Ptr
:= Sloc
(N
);
853 X_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(X
)));
854 Y_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Y
)));
855 Z_Size
: constant Int
:= UI_To_Int
(Esize
(Etype
(Z
)));
867 -- Find type that will allow computation of numerator
869 QR_Siz
:= Int
'Max (X_Size
, 2 * Int
'Max (Y_Size
, Z_Size
));
872 QR_Typ
:= Standard_Integer_16
;
873 elsif QR_Siz
<= 32 then
874 QR_Typ
:= Standard_Integer_32
;
875 elsif QR_Siz
<= 64 then
876 QR_Typ
:= Standard_Integer_64
;
878 -- For more than 64, bits, we use the 64-bit integer defined in
879 -- Interfaces, so that it can be handled by the runtime routine
882 QR_Typ
:= RTE
(RE_Integer_64
);
885 -- Define quotient and remainder, and set their Etypes, so
886 -- that they can be picked up by Build_xxx routines.
888 Qnn
:= Make_Temporary
(Loc
, 'S');
889 Rnn
:= Make_Temporary
(Loc
, 'R');
891 Set_Etype
(Qnn
, QR_Typ
);
892 Set_Etype
(Rnn
, QR_Typ
);
894 -- Case that we can compute the numerator in 64 bits
897 Nnn
:= Make_Temporary
(Loc
, 'N');
898 Dnn
:= Make_Temporary
(Loc
, 'D');
900 -- Set Etypes, so that they can be picked up by New_Occurrence_Of
902 Set_Etype
(Nnn
, QR_Typ
);
903 Set_Etype
(Dnn
, QR_Typ
);
906 Make_Object_Declaration
(Loc
,
907 Defining_Identifier
=> Nnn
,
908 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
909 Constant_Present
=> True,
912 Build_Conversion
(N
, QR_Typ
, X
),
913 Build_Conversion
(N
, QR_Typ
, Y
))),
915 Make_Object_Declaration
(Loc
,
916 Defining_Identifier
=> Dnn
,
917 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
918 Constant_Present
=> True,
919 Expression
=> Build_Conversion
(N
, QR_Typ
, Z
)));
923 New_Occurrence_Of
(Nnn
, Loc
),
924 New_Occurrence_Of
(Dnn
, Loc
));
927 Make_Object_Declaration
(Loc
,
928 Defining_Identifier
=> Qnn
,
929 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
930 Constant_Present
=> True,
934 Make_Object_Declaration
(Loc
,
935 Defining_Identifier
=> Rnn
,
936 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
),
937 Constant_Present
=> True,
940 New_Occurrence_Of
(Nnn
, Loc
),
941 New_Occurrence_Of
(Dnn
, Loc
))));
943 -- Case where numerator does not fit in 64 bits, so we have to
944 -- call the runtime routine to compute the quotient and remainder
947 Rnd
:= Boolean_Literals
(Rounded_Result_Set
(N
));
950 Make_Object_Declaration
(Loc
,
951 Defining_Identifier
=> Qnn
,
952 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
)),
954 Make_Object_Declaration
(Loc
,
955 Defining_Identifier
=> Rnn
,
956 Object_Definition
=> New_Occurrence_Of
(QR_Typ
, Loc
)),
958 Make_Procedure_Call_Statement
(Loc
,
959 Name
=> New_Occurrence_Of
(RTE
(RE_Scaled_Divide
), Loc
),
960 Parameter_Associations
=> New_List
(
961 Build_Conversion
(N
, QR_Typ
, X
),
962 Build_Conversion
(N
, QR_Typ
, Y
),
963 Build_Conversion
(N
, QR_Typ
, Z
),
964 New_Occurrence_Of
(Qnn
, Loc
),
965 New_Occurrence_Of
(Rnn
, Loc
),
966 New_Occurrence_Of
(Rnd
, Loc
))));
969 -- Set type of result, for use in caller
971 Set_Etype
(Qnn
, QR_Typ
);
972 end Build_Scaled_Divide_Code
;
974 ---------------------------
975 -- Do_Divide_Fixed_Fixed --
976 ---------------------------
980 -- (Result_Value * Result_Small) =
981 -- (Left_Value * Left_Small) / (Right_Value * Right_Small)
983 -- Result_Value = (Left_Value / Right_Value) *
984 -- (Left_Small / (Right_Small * Result_Small));
986 -- we can do the operation in integer arithmetic if this fraction is an
987 -- integer or the reciprocal of an integer, as detailed in (RM G.2.3(21)).
988 -- Otherwise the result is in the close result set and our approach is to
989 -- use floating-point to compute this close result.
991 procedure Do_Divide_Fixed_Fixed
(N
: Node_Id
) is
992 Left
: constant Node_Id
:= Left_Opnd
(N
);
993 Right
: constant Node_Id
:= Right_Opnd
(N
);
994 Left_Type
: constant Entity_Id
:= Etype
(Left
);
995 Right_Type
: constant Entity_Id
:= Etype
(Right
);
996 Result_Type
: constant Entity_Id
:= Etype
(N
);
997 Right_Small
: constant Ureal
:= Small_Value
(Right_Type
);
998 Left_Small
: constant Ureal
:= Small_Value
(Left_Type
);
1000 Result_Small
: Ureal
;
1007 -- Rounding is required if the result is integral
1009 if Is_Integer_Type
(Result_Type
) then
1010 Set_Rounded_Result
(N
);
1013 -- Get result small. If the result is an integer, treat it as though
1014 -- it had a small of 1.0, all other processing is identical.
1016 if Is_Integer_Type
(Result_Type
) then
1017 Result_Small
:= Ureal_1
;
1019 Result_Small
:= Small_Value
(Result_Type
);
1024 Frac
:= Left_Small
/ (Right_Small
* Result_Small
);
1025 Frac_Num
:= Norm_Num
(Frac
);
1026 Frac_Den
:= Norm_Den
(Frac
);
1028 -- If the fraction is an integer, then we get the result by multiplying
1029 -- the left operand by the integer, and then dividing by the right
1030 -- operand (the order is important, if we did the divide first, we
1031 -- would lose precision).
1033 if Frac_Den
= 1 then
1034 Lit_Int
:= Integer_Literal
(N
, Frac_Num
); -- always positive
1036 if Present
(Lit_Int
) then
1037 Set_Result
(N
, Build_Scaled_Divide
(N
, Left
, Lit_Int
, Right
));
1041 -- If the fraction is the reciprocal of an integer, then we get the
1042 -- result by first multiplying the divisor by the integer, and then
1043 -- doing the division with the adjusted divisor.
1045 -- Note: this is much better than doing two divisions: multiplications
1046 -- are much faster than divisions (and certainly faster than rounded
1047 -- divisions), and we don't get inaccuracies from double rounding.
1049 elsif Frac_Num
= 1 then
1050 Lit_Int
:= Integer_Literal
(N
, Frac_Den
); -- always positive
1052 if Present
(Lit_Int
) then
1053 Set_Result
(N
, Build_Double_Divide
(N
, Left
, Right
, Lit_Int
));
1058 -- If we fall through, we use floating-point to compute the result
1062 Build_Divide
(N
, Fpt_Value
(Left
), Fpt_Value
(Right
)),
1063 Real_Literal
(N
, Frac
)));
1064 end Do_Divide_Fixed_Fixed
;
1066 -------------------------------
1067 -- Do_Divide_Fixed_Universal --
1068 -------------------------------
1072 -- (Result_Value * Result_Small) = (Left_Value * Left_Small) / Lit_Value;
1073 -- Result_Value = Left_Value * Left_Small /(Lit_Value * Result_Small);
1075 -- The result is required to be in the perfect result set if the literal
1076 -- can be factored so that the resulting small ratio is an integer or the
1077 -- reciprocal of an integer (RM G.2.3(21-22)). We now give a detailed
1078 -- analysis of these RM requirements:
1080 -- We must factor the literal, finding an integer K:
1082 -- Lit_Value = K * Right_Small
1083 -- Right_Small = Lit_Value / K
1085 -- such that the small ratio:
1088 -- ------------------------------
1089 -- (Lit_Value / K) * Result_Small
1092 -- = ------------------------ * K
1093 -- Lit_Value * Result_Small
1095 -- is an integer or the reciprocal of an integer, and for
1096 -- implementation efficiency we need the smallest such K.
1098 -- First we reduce the left fraction to lowest terms
1100 -- If numerator = 1, then for K = 1, the small ratio is the reciprocal
1101 -- of an integer, and this is clearly the minimum K case, so set K = 1,
1102 -- Right_Small = Lit_Value.
1104 -- If numerator > 1, then set K to the denominator of the fraction so
1105 -- that the resulting small ratio is an integer (the numerator value).
1107 procedure Do_Divide_Fixed_Universal
(N
: Node_Id
) is
1108 Left
: constant Node_Id
:= Left_Opnd
(N
);
1109 Right
: constant Node_Id
:= Right_Opnd
(N
);
1110 Left_Type
: constant Entity_Id
:= Etype
(Left
);
1111 Result_Type
: constant Entity_Id
:= Etype
(N
);
1112 Left_Small
: constant Ureal
:= Small_Value
(Left_Type
);
1113 Lit_Value
: constant Ureal
:= Realval
(Right
);
1115 Result_Small
: Ureal
;
1123 -- Get result small. If the result is an integer, treat it as though
1124 -- it had a small of 1.0, all other processing is identical.
1126 if Is_Integer_Type
(Result_Type
) then
1127 Result_Small
:= Ureal_1
;
1129 Result_Small
:= Small_Value
(Result_Type
);
1132 -- Determine if literal can be rewritten successfully
1134 Frac
:= Left_Small
/ (Lit_Value
* Result_Small
);
1135 Frac_Num
:= Norm_Num
(Frac
);
1136 Frac_Den
:= Norm_Den
(Frac
);
1138 -- Case where fraction is the reciprocal of an integer (K = 1, integer
1139 -- = denominator). If this integer is not too large, this is the case
1140 -- where the result can be obtained by dividing by this integer value.
1142 if Frac_Num
= 1 then
1143 Lit_Int
:= Integer_Literal
(N
, Frac_Den
, UR_Is_Negative
(Frac
));
1145 if Present
(Lit_Int
) then
1146 Set_Result
(N
, Build_Divide
(N
, Left
, Lit_Int
));
1150 -- Case where we choose K to make fraction an integer (K = denominator
1151 -- of fraction, integer = numerator of fraction). If both K and the
1152 -- numerator are small enough, this is the case where the result can
1153 -- be obtained by first multiplying by the integer value and then
1154 -- dividing by K (the order is important, if we divided first, we
1155 -- would lose precision).
1158 Lit_Int
:= Integer_Literal
(N
, Frac_Num
, UR_Is_Negative
(Frac
));
1159 Lit_K
:= Integer_Literal
(N
, Frac_Den
, False);
1161 if Present
(Lit_Int
) and then Present
(Lit_K
) then
1162 Set_Result
(N
, Build_Scaled_Divide
(N
, Left
, Lit_Int
, Lit_K
));
1167 -- Fall through if the literal cannot be successfully rewritten, or if
1168 -- the small ratio is out of range of integer arithmetic. In the former
1169 -- case it is fine to use floating-point to get the close result set,
1170 -- and in the latter case, it means that the result is zero or raises
1171 -- constraint error, and we can do that accurately in floating-point.
1173 -- If we end up using floating-point, then we take the right integer
1174 -- to be one, and its small to be the value of the original right real
1175 -- literal. That way, we need only one floating-point multiplication.
1178 Build_Multiply
(N
, Fpt_Value
(Left
), Real_Literal
(N
, Frac
)));
1179 end Do_Divide_Fixed_Universal
;
1181 -------------------------------
1182 -- Do_Divide_Universal_Fixed --
1183 -------------------------------
1187 -- (Result_Value * Result_Small) =
1188 -- Lit_Value / (Right_Value * Right_Small)
1190 -- (Lit_Value / (Right_Small * Result_Small)) / Right_Value
1192 -- The result is required to be in the perfect result set if the literal
1193 -- can be factored so that the resulting small ratio is an integer or the
1194 -- reciprocal of an integer (RM G.2.3(21-22)). We now give a detailed
1195 -- analysis of these RM requirements:
1197 -- We must factor the literal, finding an integer K:
1199 -- Lit_Value = K * Left_Small
1200 -- Left_Small = Lit_Value / K
1202 -- such that the small ratio:
1205 -- --------------------------
1206 -- Right_Small * Result_Small
1209 -- = -------------------------- * -
1210 -- Right_Small * Result_Small K
1212 -- is an integer or the reciprocal of an integer, and for
1213 -- implementation efficiency we need the smallest such K.
1215 -- First we reduce the left fraction to lowest terms
1217 -- If denominator = 1, then for K = 1, the small ratio is an integer
1218 -- (the numerator) and this is clearly the minimum K case, so set K = 1,
1219 -- and Left_Small = Lit_Value.
1221 -- If denominator > 1, then set K to the numerator of the fraction so
1222 -- that the resulting small ratio is the reciprocal of an integer (the
1223 -- numerator value).
1225 procedure Do_Divide_Universal_Fixed
(N
: Node_Id
) is
1226 Left
: constant Node_Id
:= Left_Opnd
(N
);
1227 Right
: constant Node_Id
:= Right_Opnd
(N
);
1228 Right_Type
: constant Entity_Id
:= Etype
(Right
);
1229 Result_Type
: constant Entity_Id
:= Etype
(N
);
1230 Right_Small
: constant Ureal
:= Small_Value
(Right_Type
);
1231 Lit_Value
: constant Ureal
:= Realval
(Left
);
1233 Result_Small
: Ureal
;
1241 -- Get result small. If the result is an integer, treat it as though
1242 -- it had a small of 1.0, all other processing is identical.
1244 if Is_Integer_Type
(Result_Type
) then
1245 Result_Small
:= Ureal_1
;
1247 Result_Small
:= Small_Value
(Result_Type
);
1250 -- Determine if literal can be rewritten successfully
1252 Frac
:= Lit_Value
/ (Right_Small
* Result_Small
);
1253 Frac_Num
:= Norm_Num
(Frac
);
1254 Frac_Den
:= Norm_Den
(Frac
);
1256 -- Case where fraction is an integer (K = 1, integer = numerator). If
1257 -- this integer is not too large, this is the case where the result
1258 -- can be obtained by dividing this integer by the right operand.
1260 if Frac_Den
= 1 then
1261 Lit_Int
:= Integer_Literal
(N
, Frac_Num
, UR_Is_Negative
(Frac
));
1263 if Present
(Lit_Int
) then
1264 Set_Result
(N
, Build_Divide
(N
, Lit_Int
, Right
));
1268 -- Case where we choose K to make the fraction the reciprocal of an
1269 -- integer (K = numerator of fraction, integer = numerator of fraction).
1270 -- If both K and the integer are small enough, this is the case where
1271 -- the result can be obtained by multiplying the right operand by K
1272 -- and then dividing by the integer value. The order of the operations
1273 -- is important (if we divided first, we would lose precision).
1276 Lit_Int
:= Integer_Literal
(N
, Frac_Den
, UR_Is_Negative
(Frac
));
1277 Lit_K
:= Integer_Literal
(N
, Frac_Num
, False);
1279 if Present
(Lit_Int
) and then Present
(Lit_K
) then
1280 Set_Result
(N
, Build_Double_Divide
(N
, Lit_K
, Right
, Lit_Int
));
1285 -- Fall through if the literal cannot be successfully rewritten, or if
1286 -- the small ratio is out of range of integer arithmetic. In the former
1287 -- case it is fine to use floating-point to get the close result set,
1288 -- and in the latter case, it means that the result is zero or raises
1289 -- constraint error, and we can do that accurately in floating-point.
1291 -- If we end up using floating-point, then we take the right integer
1292 -- to be one, and its small to be the value of the original right real
1293 -- literal. That way, we need only one floating-point division.
1296 Build_Divide
(N
, Real_Literal
(N
, Frac
), Fpt_Value
(Right
)));
1297 end Do_Divide_Universal_Fixed
;
1299 -----------------------------
1300 -- Do_Multiply_Fixed_Fixed --
1301 -----------------------------
1305 -- (Result_Value * Result_Small) =
1306 -- (Left_Value * Left_Small) * (Right_Value * Right_Small)
1308 -- Result_Value = (Left_Value * Right_Value) *
1309 -- (Left_Small * Right_Small) / Result_Small;
1311 -- we can do the operation in integer arithmetic if this fraction is an
1312 -- integer or the reciprocal of an integer, as detailed in (RM G.2.3(21)).
1313 -- Otherwise the result is in the close result set and our approach is to
1314 -- use floating-point to compute this close result.
1316 procedure Do_Multiply_Fixed_Fixed
(N
: Node_Id
) is
1317 Left
: constant Node_Id
:= Left_Opnd
(N
);
1318 Right
: constant Node_Id
:= Right_Opnd
(N
);
1320 Left_Type
: constant Entity_Id
:= Etype
(Left
);
1321 Right_Type
: constant Entity_Id
:= Etype
(Right
);
1322 Result_Type
: constant Entity_Id
:= Etype
(N
);
1323 Right_Small
: constant Ureal
:= Small_Value
(Right_Type
);
1324 Left_Small
: constant Ureal
:= Small_Value
(Left_Type
);
1326 Result_Small
: Ureal
;
1333 -- Get result small. If the result is an integer, treat it as though
1334 -- it had a small of 1.0, all other processing is identical.
1336 if Is_Integer_Type
(Result_Type
) then
1337 Result_Small
:= Ureal_1
;
1339 Result_Small
:= Small_Value
(Result_Type
);
1344 Frac
:= (Left_Small
* Right_Small
) / Result_Small
;
1345 Frac_Num
:= Norm_Num
(Frac
);
1346 Frac_Den
:= Norm_Den
(Frac
);
1348 -- If the fraction is an integer, then we get the result by multiplying
1349 -- the operands, and then multiplying the result by the integer value.
1351 if Frac_Den
= 1 then
1352 Lit_Int
:= Integer_Literal
(N
, Frac_Num
); -- always positive
1354 if Present
(Lit_Int
) then
1356 Build_Multiply
(N
, Build_Multiply
(N
, Left
, Right
),
1361 -- If the fraction is the reciprocal of an integer, then we get the
1362 -- result by multiplying the operands, and then dividing the result by
1363 -- the integer value. The order of the operations is important, if we
1364 -- divided first, we would lose precision.
1366 elsif Frac_Num
= 1 then
1367 Lit_Int
:= Integer_Literal
(N
, Frac_Den
); -- always positive
1369 if Present
(Lit_Int
) then
1370 Set_Result
(N
, Build_Scaled_Divide
(N
, Left
, Right
, Lit_Int
));
1375 -- If we fall through, we use floating-point to compute the result
1379 Build_Multiply
(N
, Fpt_Value
(Left
), Fpt_Value
(Right
)),
1380 Real_Literal
(N
, Frac
)));
1381 end Do_Multiply_Fixed_Fixed
;
1383 ---------------------------------
1384 -- Do_Multiply_Fixed_Universal --
1385 ---------------------------------
1389 -- (Result_Value * Result_Small) = (Left_Value * Left_Small) * Lit_Value;
1390 -- Result_Value = Left_Value * (Left_Small * Lit_Value) / Result_Small;
1392 -- The result is required to be in the perfect result set if the literal
1393 -- can be factored so that the resulting small ratio is an integer or the
1394 -- reciprocal of an integer (RM G.2.3(21-22)). We now give a detailed
1395 -- analysis of these RM requirements:
1397 -- We must factor the literal, finding an integer K:
1399 -- Lit_Value = K * Right_Small
1400 -- Right_Small = Lit_Value / K
1402 -- such that the small ratio:
1404 -- Left_Small * (Lit_Value / K)
1405 -- ----------------------------
1408 -- Left_Small * Lit_Value 1
1409 -- = ---------------------- * -
1412 -- is an integer or the reciprocal of an integer, and for
1413 -- implementation efficiency we need the smallest such K.
1415 -- First we reduce the left fraction to lowest terms
1417 -- If denominator = 1, then for K = 1, the small ratio is an integer, and
1418 -- this is clearly the minimum K case, so set
1420 -- K = 1, Right_Small = Lit_Value
1422 -- If denominator > 1, then set K to the numerator of the fraction, so
1423 -- that the resulting small ratio is the reciprocal of the integer (the
1424 -- denominator value).
1426 procedure Do_Multiply_Fixed_Universal
1428 Left
, Right
: Node_Id
)
1430 Left_Type
: constant Entity_Id
:= Etype
(Left
);
1431 Result_Type
: constant Entity_Id
:= Etype
(N
);
1432 Left_Small
: constant Ureal
:= Small_Value
(Left_Type
);
1433 Lit_Value
: constant Ureal
:= Realval
(Right
);
1435 Result_Small
: Ureal
;
1443 -- Get result small. If the result is an integer, treat it as though
1444 -- it had a small of 1.0, all other processing is identical.
1446 if Is_Integer_Type
(Result_Type
) then
1447 Result_Small
:= Ureal_1
;
1449 Result_Small
:= Small_Value
(Result_Type
);
1452 -- Determine if literal can be rewritten successfully
1454 Frac
:= (Left_Small
* Lit_Value
) / Result_Small
;
1455 Frac_Num
:= Norm_Num
(Frac
);
1456 Frac_Den
:= Norm_Den
(Frac
);
1458 -- Case where fraction is an integer (K = 1, integer = numerator). If
1459 -- this integer is not too large, this is the case where the result can
1460 -- be obtained by multiplying by this integer value.
1462 if Frac_Den
= 1 then
1463 Lit_Int
:= Integer_Literal
(N
, Frac_Num
, UR_Is_Negative
(Frac
));
1465 if Present
(Lit_Int
) then
1466 Set_Result
(N
, Build_Multiply
(N
, Left
, Lit_Int
));
1470 -- Case where we choose K to make fraction the reciprocal of an integer
1471 -- (K = numerator of fraction, integer = denominator of fraction). If
1472 -- both K and the denominator are small enough, this is the case where
1473 -- the result can be obtained by first multiplying by K, and then
1474 -- dividing by the integer value.
1477 Lit_Int
:= Integer_Literal
(N
, Frac_Den
, UR_Is_Negative
(Frac
));
1478 Lit_K
:= Integer_Literal
(N
, Frac_Num
);
1480 if Present
(Lit_Int
) and then Present
(Lit_K
) then
1481 Set_Result
(N
, Build_Scaled_Divide
(N
, Left
, Lit_K
, Lit_Int
));
1486 -- Fall through if the literal cannot be successfully rewritten, or if
1487 -- the small ratio is out of range of integer arithmetic. In the former
1488 -- case it is fine to use floating-point to get the close result set,
1489 -- and in the latter case, it means that the result is zero or raises
1490 -- constraint error, and we can do that accurately in floating-point.
1492 -- If we end up using floating-point, then we take the right integer
1493 -- to be one, and its small to be the value of the original right real
1494 -- literal. That way, we need only one floating-point multiplication.
1497 Build_Multiply
(N
, Fpt_Value
(Left
), Real_Literal
(N
, Frac
)));
1498 end Do_Multiply_Fixed_Universal
;
1500 ---------------------------------
1501 -- Expand_Convert_Fixed_Static --
1502 ---------------------------------
1504 procedure Expand_Convert_Fixed_Static
(N
: Node_Id
) is
1507 Convert_To
(Etype
(N
),
1508 Make_Real_Literal
(Sloc
(N
), Expr_Value_R
(Expression
(N
)))));
1509 Analyze_And_Resolve
(N
);
1510 end Expand_Convert_Fixed_Static
;
1512 -----------------------------------
1513 -- Expand_Convert_Fixed_To_Fixed --
1514 -----------------------------------
1518 -- Result_Value * Result_Small = Source_Value * Source_Small
1519 -- Result_Value = Source_Value * (Source_Small / Result_Small)
1521 -- If the small ratio (Source_Small / Result_Small) is a sufficiently small
1522 -- integer, then the perfect result set is obtained by a single integer
1525 -- If the small ratio is the reciprocal of a sufficiently small integer,
1526 -- then the perfect result set is obtained by a single integer division.
1528 -- In other cases, we obtain the close result set by calculating the
1529 -- result in floating-point.
1531 procedure Expand_Convert_Fixed_To_Fixed
(N
: Node_Id
) is
1532 Rng_Check
: constant Boolean := Do_Range_Check
(N
);
1533 Expr
: constant Node_Id
:= Expression
(N
);
1534 Result_Type
: constant Entity_Id
:= Etype
(N
);
1535 Source_Type
: constant Entity_Id
:= Etype
(Expr
);
1536 Small_Ratio
: Ureal
;
1542 if Is_OK_Static_Expression
(Expr
) then
1543 Expand_Convert_Fixed_Static
(N
);
1547 Small_Ratio
:= Small_Value
(Source_Type
) / Small_Value
(Result_Type
);
1548 Ratio_Num
:= Norm_Num
(Small_Ratio
);
1549 Ratio_Den
:= Norm_Den
(Small_Ratio
);
1551 if Ratio_Den
= 1 then
1552 if Ratio_Num
= 1 then
1553 Set_Result
(N
, Expr
);
1557 Lit
:= Integer_Literal
(N
, Ratio_Num
);
1559 if Present
(Lit
) then
1560 Set_Result
(N
, Build_Multiply
(N
, Expr
, Lit
));
1565 elsif Ratio_Num
= 1 then
1566 Lit
:= Integer_Literal
(N
, Ratio_Den
);
1568 if Present
(Lit
) then
1569 Set_Result
(N
, Build_Divide
(N
, Expr
, Lit
), Rng_Check
);
1574 -- Fall through to use floating-point for the close result set case
1575 -- either as a result of the small ratio not being an integer or the
1576 -- reciprocal of an integer, or if the integer is out of range.
1581 Real_Literal
(N
, Small_Ratio
)),
1583 end Expand_Convert_Fixed_To_Fixed
;
1585 -----------------------------------
1586 -- Expand_Convert_Fixed_To_Float --
1587 -----------------------------------
1589 -- If the small of the fixed type is 1.0, then we simply convert the
1590 -- integer value directly to the target floating-point type, otherwise
1591 -- we first have to multiply by the small, in Universal_Real, and then
1592 -- convert the result to the target floating-point type.
1594 procedure Expand_Convert_Fixed_To_Float
(N
: Node_Id
) is
1595 Rng_Check
: constant Boolean := Do_Range_Check
(N
);
1596 Expr
: constant Node_Id
:= Expression
(N
);
1597 Source_Type
: constant Entity_Id
:= Etype
(Expr
);
1598 Small
: constant Ureal
:= Small_Value
(Source_Type
);
1601 if Is_OK_Static_Expression
(Expr
) then
1602 Expand_Convert_Fixed_Static
(N
);
1606 if Small
= Ureal_1
then
1607 Set_Result
(N
, Expr
);
1613 Real_Literal
(N
, Small
)),
1616 end Expand_Convert_Fixed_To_Float
;
1618 -------------------------------------
1619 -- Expand_Convert_Fixed_To_Integer --
1620 -------------------------------------
1624 -- Result_Value = Source_Value * Source_Small
1626 -- If the small value is a sufficiently small integer, then the perfect
1627 -- result set is obtained by a single integer multiplication.
1629 -- If the small value is the reciprocal of a sufficiently small integer,
1630 -- then the perfect result set is obtained by a single integer division.
1632 -- In other cases, we obtain the close result set by calculating the
1633 -- result in floating-point.
1635 procedure Expand_Convert_Fixed_To_Integer
(N
: Node_Id
) is
1636 Rng_Check
: constant Boolean := Do_Range_Check
(N
);
1637 Expr
: constant Node_Id
:= Expression
(N
);
1638 Source_Type
: constant Entity_Id
:= Etype
(Expr
);
1639 Small
: constant Ureal
:= Small_Value
(Source_Type
);
1640 Small_Num
: constant Uint
:= Norm_Num
(Small
);
1641 Small_Den
: constant Uint
:= Norm_Den
(Small
);
1645 if Is_OK_Static_Expression
(Expr
) then
1646 Expand_Convert_Fixed_Static
(N
);
1650 if Small_Den
= 1 then
1651 Lit
:= Integer_Literal
(N
, Small_Num
);
1653 if Present
(Lit
) then
1654 Set_Result
(N
, Build_Multiply
(N
, Expr
, Lit
), Rng_Check
);
1658 elsif Small_Num
= 1 then
1659 Lit
:= Integer_Literal
(N
, Small_Den
);
1661 if Present
(Lit
) then
1662 Set_Result
(N
, Build_Divide
(N
, Expr
, Lit
), Rng_Check
);
1667 -- Fall through to use floating-point for the close result set case
1668 -- either as a result of the small value not being an integer or the
1669 -- reciprocal of an integer, or if the integer is out of range.
1674 Real_Literal
(N
, Small
)),
1676 end Expand_Convert_Fixed_To_Integer
;
1678 -----------------------------------
1679 -- Expand_Convert_Float_To_Fixed --
1680 -----------------------------------
1684 -- Result_Value * Result_Small = Operand_Value
1688 -- Result_Value = Operand_Value * (1.0 / Result_Small)
1690 -- We do the small scaling in floating-point, and we do a multiplication
1691 -- rather than a division, since it is accurate enough for the perfect
1692 -- result cases, and faster.
1694 procedure Expand_Convert_Float_To_Fixed
(N
: Node_Id
) is
1695 Expr
: constant Node_Id
:= Expression
(N
);
1696 Orig_N
: constant Node_Id
:= Original_Node
(N
);
1697 Result_Type
: constant Entity_Id
:= Etype
(N
);
1698 Rng_Check
: constant Boolean := Do_Range_Check
(N
);
1699 Small
: constant Ureal
:= Small_Value
(Result_Type
);
1703 -- Optimize small = 1, where we can avoid the multiply completely
1705 if Small
= Ureal_1
then
1706 Set_Result
(N
, Expr
, Rng_Check
, Trunc
=> True);
1708 -- Normal case where multiply is required. Rounding is truncating
1709 -- for decimal fixed point types only, see RM 4.6(29), except if the
1710 -- conversion comes from an attribute reference 'Round (RM 3.5.10 (14)):
1711 -- The attribute is implemented by means of a conversion that must
1715 if Is_Decimal_Fixed_Point_Type
(Result_Type
) then
1717 Nkind
(Orig_N
) /= N_Attribute_Reference
1718 or else Get_Attribute_Id
1719 (Attribute_Name
(Orig_N
)) /= Attribute_Round
;
1729 L
=> Fpt_Value
(Expr
),
1730 R
=> Real_Literal
(N
, Ureal_1
/ Small
)),
1734 end Expand_Convert_Float_To_Fixed
;
1736 -------------------------------------
1737 -- Expand_Convert_Integer_To_Fixed --
1738 -------------------------------------
1742 -- Result_Value * Result_Small = Operand_Value
1743 -- Result_Value = Operand_Value / Result_Small
1745 -- If the small value is a sufficiently small integer, then the perfect
1746 -- result set is obtained by a single integer division.
1748 -- If the small value is the reciprocal of a sufficiently small integer,
1749 -- the perfect result set is obtained by a single integer multiplication.
1751 -- In other cases, we obtain the close result set by calculating the
1752 -- result in floating-point using a multiplication by the reciprocal
1753 -- of the Result_Small.
1755 procedure Expand_Convert_Integer_To_Fixed
(N
: Node_Id
) is
1756 Rng_Check
: constant Boolean := Do_Range_Check
(N
);
1757 Expr
: constant Node_Id
:= Expression
(N
);
1758 Result_Type
: constant Entity_Id
:= Etype
(N
);
1759 Small
: constant Ureal
:= Small_Value
(Result_Type
);
1760 Small_Num
: constant Uint
:= Norm_Num
(Small
);
1761 Small_Den
: constant Uint
:= Norm_Den
(Small
);
1765 if Small_Den
= 1 then
1766 Lit
:= Integer_Literal
(N
, Small_Num
);
1768 if Present
(Lit
) then
1769 Set_Result
(N
, Build_Divide
(N
, Expr
, Lit
), Rng_Check
);
1773 elsif Small_Num
= 1 then
1774 Lit
:= Integer_Literal
(N
, Small_Den
);
1776 if Present
(Lit
) then
1777 Set_Result
(N
, Build_Multiply
(N
, Expr
, Lit
), Rng_Check
);
1782 -- Fall through to use floating-point for the close result set case
1783 -- either as a result of the small value not being an integer or the
1784 -- reciprocal of an integer, or if the integer is out of range.
1789 Real_Literal
(N
, Ureal_1
/ Small
)),
1791 end Expand_Convert_Integer_To_Fixed
;
1793 --------------------------------
1794 -- Expand_Decimal_Divide_Call --
1795 --------------------------------
1797 -- We have four operands
1804 -- All of which are decimal types, and which thus have associated
1807 -- Computing the quotient is a similar problem to that faced by the
1808 -- normal fixed-point division, except that it is simpler, because
1809 -- we always have compatible smalls.
1811 -- Quotient = (Dividend / Divisor) * 10**q
1813 -- where 10 ** q = Dividend'Small / (Divisor'Small * Quotient'Small)
1814 -- so q = Divisor'Scale + Quotient'Scale - Dividend'Scale
1816 -- For q >= 0, we compute
1818 -- Numerator := Dividend * 10 ** q
1819 -- Denominator := Divisor
1820 -- Quotient := Numerator / Denominator
1822 -- For q < 0, we compute
1824 -- Numerator := Dividend
1825 -- Denominator := Divisor * 10 ** q
1826 -- Quotient := Numerator / Denominator
1828 -- Both these divisions are done in truncated mode, and the remainder
1829 -- from these divisions is used to compute the result Remainder. This
1830 -- remainder has the effective scale of the numerator of the division,
1832 -- For q >= 0, the remainder scale is Dividend'Scale + q
1833 -- For q < 0, the remainder scale is Dividend'Scale
1835 -- The result Remainder is then computed by a normal truncating decimal
1836 -- conversion from this scale to the scale of the remainder, i.e. by a
1837 -- division or multiplication by the appropriate power of 10.
1839 procedure Expand_Decimal_Divide_Call
(N
: Node_Id
) is
1840 Loc
: constant Source_Ptr
:= Sloc
(N
);
1842 Dividend
: Node_Id
:= First_Actual
(N
);
1843 Divisor
: Node_Id
:= Next_Actual
(Dividend
);
1844 Quotient
: Node_Id
:= Next_Actual
(Divisor
);
1845 Remainder
: Node_Id
:= Next_Actual
(Quotient
);
1847 Dividend_Type
: constant Entity_Id
:= Etype
(Dividend
);
1848 Divisor_Type
: constant Entity_Id
:= Etype
(Divisor
);
1849 Quotient_Type
: constant Entity_Id
:= Etype
(Quotient
);
1850 Remainder_Type
: constant Entity_Id
:= Etype
(Remainder
);
1852 Dividend_Scale
: constant Uint
:= Scale_Value
(Dividend_Type
);
1853 Divisor_Scale
: constant Uint
:= Scale_Value
(Divisor_Type
);
1854 Quotient_Scale
: constant Uint
:= Scale_Value
(Quotient_Type
);
1855 Remainder_Scale
: constant Uint
:= Scale_Value
(Remainder_Type
);
1858 Numerator_Scale
: Uint
;
1862 Computed_Remainder
: Node_Id
;
1863 Adjusted_Remainder
: Node_Id
;
1864 Scale_Adjust
: Uint
;
1867 -- Relocate the operands, since they are now list elements, and we
1868 -- need to reference them separately as operands in the expanded code.
1870 Dividend
:= Relocate_Node
(Dividend
);
1871 Divisor
:= Relocate_Node
(Divisor
);
1872 Quotient
:= Relocate_Node
(Quotient
);
1873 Remainder
:= Relocate_Node
(Remainder
);
1875 -- Now compute Q, the adjustment scale
1877 Q
:= Divisor_Scale
+ Quotient_Scale
- Dividend_Scale
;
1879 -- If Q is non-negative then we need a scaled divide
1882 Build_Scaled_Divide_Code
1885 Integer_Literal
(N
, Uint_10
** Q
),
1889 Numerator_Scale
:= Dividend_Scale
+ Q
;
1891 -- If Q is negative, then we need a double divide
1894 Build_Double_Divide_Code
1898 Integer_Literal
(N
, Uint_10
** (-Q
)),
1901 Numerator_Scale
:= Dividend_Scale
;
1904 -- Add statement to set quotient value
1906 -- Quotient := quotient-type!(Qnn);
1909 Make_Assignment_Statement
(Loc
,
1912 Unchecked_Convert_To
(Quotient_Type
,
1913 Build_Conversion
(N
, Quotient_Type
,
1914 New_Occurrence_Of
(Qnn
, Loc
)))));
1916 -- Now we need to deal with computing and setting the remainder. The
1917 -- scale of the remainder is in Numerator_Scale, and the desired
1918 -- scale is the scale of the given Remainder argument. There are
1921 -- Numerator_Scale > Remainder_Scale
1923 -- in this case, there are extra digits in the computed remainder
1924 -- which must be eliminated by an extra division:
1926 -- computed-remainder := Numerator rem Denominator
1927 -- scale_adjust = Numerator_Scale - Remainder_Scale
1928 -- adjusted-remainder := computed-remainder / 10 ** scale_adjust
1930 -- Numerator_Scale = Remainder_Scale
1932 -- in this case, the we have the remainder we need
1934 -- computed-remainder := Numerator rem Denominator
1935 -- adjusted-remainder := computed-remainder
1937 -- Numerator_Scale < Remainder_Scale
1939 -- in this case, we have insufficient digits in the computed
1940 -- remainder, which must be eliminated by an extra multiply
1942 -- computed-remainder := Numerator rem Denominator
1943 -- scale_adjust = Remainder_Scale - Numerator_Scale
1944 -- adjusted-remainder := computed-remainder * 10 ** scale_adjust
1946 -- Finally we assign the adjusted-remainder to the result Remainder
1947 -- with conversions to get the proper fixed-point type representation.
1949 Computed_Remainder
:= New_Occurrence_Of
(Rnn
, Loc
);
1951 if Numerator_Scale
> Remainder_Scale
then
1952 Scale_Adjust
:= Numerator_Scale
- Remainder_Scale
;
1953 Adjusted_Remainder
:=
1955 (N
, Computed_Remainder
, Integer_Literal
(N
, 10 ** Scale_Adjust
));
1957 elsif Numerator_Scale
= Remainder_Scale
then
1958 Adjusted_Remainder
:= Computed_Remainder
;
1960 else -- Numerator_Scale < Remainder_Scale
1961 Scale_Adjust
:= Remainder_Scale
- Numerator_Scale
;
1962 Adjusted_Remainder
:=
1964 (N
, Computed_Remainder
, Integer_Literal
(N
, 10 ** Scale_Adjust
));
1967 -- Assignment of remainder result
1970 Make_Assignment_Statement
(Loc
,
1973 Unchecked_Convert_To
(Remainder_Type
, Adjusted_Remainder
)));
1975 -- Final step is to rewrite the call with a block containing the
1976 -- above sequence of constructed statements for the divide operation.
1979 Make_Block_Statement
(Loc
,
1980 Handled_Statement_Sequence
=>
1981 Make_Handled_Sequence_Of_Statements
(Loc
,
1982 Statements
=> Stmts
)));
1985 end Expand_Decimal_Divide_Call
;
1987 -----------------------------------------------
1988 -- Expand_Divide_Fixed_By_Fixed_Giving_Fixed --
1989 -----------------------------------------------
1991 procedure Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
: Node_Id
) is
1992 Left
: constant Node_Id
:= Left_Opnd
(N
);
1993 Right
: constant Node_Id
:= Right_Opnd
(N
);
1996 -- Suppress expansion of a fixed-by-fixed division if the
1997 -- operation is supported directly by the target.
1999 if Target_Has_Fixed_Ops
(Etype
(Left
), Etype
(Right
), Etype
(N
)) then
2003 if Etype
(Left
) = Universal_Real
then
2004 Do_Divide_Universal_Fixed
(N
);
2006 elsif Etype
(Right
) = Universal_Real
then
2007 Do_Divide_Fixed_Universal
(N
);
2010 Do_Divide_Fixed_Fixed
(N
);
2012 end Expand_Divide_Fixed_By_Fixed_Giving_Fixed
;
2014 -----------------------------------------------
2015 -- Expand_Divide_Fixed_By_Fixed_Giving_Float --
2016 -----------------------------------------------
2018 -- The division is done in Universal_Real, and the result is multiplied
2019 -- by the small ratio, which is Small (Right) / Small (Left). Special
2020 -- treatment is required for universal operands, which represent their
2021 -- own value and do not require conversion.
2023 procedure Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
: Node_Id
) is
2024 Left
: constant Node_Id
:= Left_Opnd
(N
);
2025 Right
: constant Node_Id
:= Right_Opnd
(N
);
2027 Left_Type
: constant Entity_Id
:= Etype
(Left
);
2028 Right_Type
: constant Entity_Id
:= Etype
(Right
);
2031 -- Case of left operand is universal real, the result we want is:
2033 -- Left_Value / (Right_Value * Right_Small)
2035 -- so we compute this as:
2037 -- (Left_Value / Right_Small) / Right_Value
2039 if Left_Type
= Universal_Real
then
2042 Real_Literal
(N
, Realval
(Left
) / Small_Value
(Right_Type
)),
2043 Fpt_Value
(Right
)));
2045 -- Case of right operand is universal real, the result we want is
2047 -- (Left_Value * Left_Small) / Right_Value
2049 -- so we compute this as:
2051 -- Left_Value * (Left_Small / Right_Value)
2053 -- Note we invert to a multiplication since usually floating-point
2054 -- multiplication is much faster than floating-point division.
2056 elsif Right_Type
= Universal_Real
then
2060 Real_Literal
(N
, Small_Value
(Left_Type
) / Realval
(Right
))));
2062 -- Both operands are fixed, so the value we want is
2064 -- (Left_Value * Left_Small) / (Right_Value * Right_Small)
2066 -- which we compute as:
2068 -- (Left_Value / Right_Value) * (Left_Small / Right_Small)
2073 Build_Divide
(N
, Fpt_Value
(Left
), Fpt_Value
(Right
)),
2075 Small_Value
(Left_Type
) / Small_Value
(Right_Type
))));
2077 end Expand_Divide_Fixed_By_Fixed_Giving_Float
;
2079 -------------------------------------------------
2080 -- Expand_Divide_Fixed_By_Fixed_Giving_Integer --
2081 -------------------------------------------------
2083 procedure Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
: Node_Id
) is
2084 Left
: constant Node_Id
:= Left_Opnd
(N
);
2085 Right
: constant Node_Id
:= Right_Opnd
(N
);
2087 if Etype
(Left
) = Universal_Real
then
2088 Do_Divide_Universal_Fixed
(N
);
2089 elsif Etype
(Right
) = Universal_Real
then
2090 Do_Divide_Fixed_Universal
(N
);
2092 Do_Divide_Fixed_Fixed
(N
);
2094 end Expand_Divide_Fixed_By_Fixed_Giving_Integer
;
2096 -------------------------------------------------
2097 -- Expand_Divide_Fixed_By_Integer_Giving_Fixed --
2098 -------------------------------------------------
2100 -- Since the operand and result fixed-point type is the same, this is
2101 -- a straight divide by the right operand, the small can be ignored.
2103 procedure Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
: Node_Id
) is
2104 Left
: constant Node_Id
:= Left_Opnd
(N
);
2105 Right
: constant Node_Id
:= Right_Opnd
(N
);
2107 Set_Result
(N
, Build_Divide
(N
, Left
, Right
));
2108 end Expand_Divide_Fixed_By_Integer_Giving_Fixed
;
2110 -------------------------------------------------
2111 -- Expand_Multiply_Fixed_By_Fixed_Giving_Fixed --
2112 -------------------------------------------------
2114 procedure Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
: Node_Id
) is
2115 Left
: constant Node_Id
:= Left_Opnd
(N
);
2116 Right
: constant Node_Id
:= Right_Opnd
(N
);
2118 procedure Rewrite_Non_Static_Universal
(Opnd
: Node_Id
);
2119 -- The operand may be a non-static universal value, such an
2120 -- exponentiation with a non-static exponent. In that case, treat
2121 -- as a fixed * fixed multiplication, and convert the argument to
2122 -- the target fixed type.
2124 ----------------------------------
2125 -- Rewrite_Non_Static_Universal --
2126 ----------------------------------
2128 procedure Rewrite_Non_Static_Universal
(Opnd
: Node_Id
) is
2129 Loc
: constant Source_Ptr
:= Sloc
(N
);
2132 Make_Type_Conversion
(Loc
,
2133 Subtype_Mark
=> New_Occurrence_Of
(Etype
(N
), Loc
),
2134 Expression
=> Expression
(Opnd
)));
2135 Analyze_And_Resolve
(Opnd
, Etype
(N
));
2136 end Rewrite_Non_Static_Universal
;
2138 -- Start of processing for Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
2141 -- Suppress expansion of a fixed-by-fixed multiplication if the
2142 -- operation is supported directly by the target.
2144 if Target_Has_Fixed_Ops
(Etype
(Left
), Etype
(Right
), Etype
(N
)) then
2148 if Etype
(Left
) = Universal_Real
then
2149 if Nkind
(Left
) = N_Real_Literal
then
2150 Do_Multiply_Fixed_Universal
(N
, Left
=> Right
, Right
=> Left
);
2152 elsif Nkind
(Left
) = N_Type_Conversion
then
2153 Rewrite_Non_Static_Universal
(Left
);
2154 Do_Multiply_Fixed_Fixed
(N
);
2157 elsif Etype
(Right
) = Universal_Real
then
2158 if Nkind
(Right
) = N_Real_Literal
then
2159 Do_Multiply_Fixed_Universal
(N
, Left
, Right
);
2161 elsif Nkind
(Right
) = N_Type_Conversion
then
2162 Rewrite_Non_Static_Universal
(Right
);
2163 Do_Multiply_Fixed_Fixed
(N
);
2167 Do_Multiply_Fixed_Fixed
(N
);
2169 end Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
;
2171 -------------------------------------------------
2172 -- Expand_Multiply_Fixed_By_Fixed_Giving_Float --
2173 -------------------------------------------------
2175 -- The multiply is done in Universal_Real, and the result is multiplied
2176 -- by the adjustment for the smalls which is Small (Right) * Small (Left).
2177 -- Special treatment is required for universal operands.
2179 procedure Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
: Node_Id
) is
2180 Left
: constant Node_Id
:= Left_Opnd
(N
);
2181 Right
: constant Node_Id
:= Right_Opnd
(N
);
2183 Left_Type
: constant Entity_Id
:= Etype
(Left
);
2184 Right_Type
: constant Entity_Id
:= Etype
(Right
);
2187 -- Case of left operand is universal real, the result we want is
2189 -- Left_Value * (Right_Value * Right_Small)
2191 -- so we compute this as:
2193 -- (Left_Value * Right_Small) * Right_Value;
2195 if Left_Type
= Universal_Real
then
2198 Real_Literal
(N
, Realval
(Left
) * Small_Value
(Right_Type
)),
2199 Fpt_Value
(Right
)));
2201 -- Case of right operand is universal real, the result we want is
2203 -- (Left_Value * Left_Small) * Right_Value
2205 -- so we compute this as:
2207 -- Left_Value * (Left_Small * Right_Value)
2209 elsif Right_Type
= Universal_Real
then
2213 Real_Literal
(N
, Small_Value
(Left_Type
) * Realval
(Right
))));
2215 -- Both operands are fixed, so the value we want is
2217 -- (Left_Value * Left_Small) * (Right_Value * Right_Small)
2219 -- which we compute as:
2221 -- (Left_Value * Right_Value) * (Right_Small * Left_Small)
2226 Build_Multiply
(N
, Fpt_Value
(Left
), Fpt_Value
(Right
)),
2228 Small_Value
(Right_Type
) * Small_Value
(Left_Type
))));
2230 end Expand_Multiply_Fixed_By_Fixed_Giving_Float
;
2232 ---------------------------------------------------
2233 -- Expand_Multiply_Fixed_By_Fixed_Giving_Integer --
2234 ---------------------------------------------------
2236 procedure Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
: Node_Id
) is
2237 Loc
: constant Source_Ptr
:= Sloc
(N
);
2238 Left
: constant Node_Id
:= Left_Opnd
(N
);
2239 Right
: constant Node_Id
:= Right_Opnd
(N
);
2242 if Etype
(Left
) = Universal_Real
then
2243 Do_Multiply_Fixed_Universal
(N
, Left
=> Right
, Right
=> Left
);
2245 elsif Etype
(Right
) = Universal_Real
then
2246 Do_Multiply_Fixed_Universal
(N
, Left
, Right
);
2248 -- If both types are equal and we need to avoid floating point
2249 -- instructions, it's worth introducing a temporary with the
2250 -- common type, because it may be evaluated more simply without
2251 -- the need for run-time use of floating point.
2253 elsif Etype
(Right
) = Etype
(Left
)
2254 and then Restriction_Active
(No_Floating_Point
)
2257 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F');
2258 Mult
: constant Node_Id
:= Make_Op_Multiply
(Loc
, Left
, Right
);
2259 Decl
: constant Node_Id
:=
2260 Make_Object_Declaration
(Loc
,
2261 Defining_Identifier
=> Temp
,
2262 Object_Definition
=> New_Occurrence_Of
(Etype
(Right
), Loc
),
2263 Expression
=> Mult
);
2266 Insert_Action
(N
, Decl
);
2268 OK_Convert_To
(Etype
(N
), New_Occurrence_Of
(Temp
, Loc
)));
2269 Analyze_And_Resolve
(N
, Standard_Integer
);
2273 Do_Multiply_Fixed_Fixed
(N
);
2275 end Expand_Multiply_Fixed_By_Fixed_Giving_Integer
;
2277 ---------------------------------------------------
2278 -- Expand_Multiply_Fixed_By_Integer_Giving_Fixed --
2279 ---------------------------------------------------
2281 -- Since the operand and result fixed-point type is the same, this is
2282 -- a straight multiply by the right operand, the small can be ignored.
2284 procedure Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
: Node_Id
) is
2287 Build_Multiply
(N
, Left_Opnd
(N
), Right_Opnd
(N
)));
2288 end Expand_Multiply_Fixed_By_Integer_Giving_Fixed
;
2290 ---------------------------------------------------
2291 -- Expand_Multiply_Integer_By_Fixed_Giving_Fixed --
2292 ---------------------------------------------------
2294 -- Since the operand and result fixed-point type is the same, this is
2295 -- a straight multiply by the right operand, the small can be ignored.
2297 procedure Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
: Node_Id
) is
2300 Build_Multiply
(N
, Left_Opnd
(N
), Right_Opnd
(N
)));
2301 end Expand_Multiply_Integer_By_Fixed_Giving_Fixed
;
2307 function Fpt_Value
(N
: Node_Id
) return Node_Id
is
2308 Typ
: constant Entity_Id
:= Etype
(N
);
2311 if Is_Integer_Type
(Typ
)
2312 or else Is_Floating_Point_Type
(Typ
)
2314 return Build_Conversion
(N
, Universal_Real
, N
);
2316 -- Fixed-point case, must get integer value first
2319 return Build_Conversion
(N
, Universal_Real
, N
);
2323 ---------------------
2324 -- Integer_Literal --
2325 ---------------------
2327 function Integer_Literal
2330 Negative
: Boolean := False) return Node_Id
2336 if V
< Uint_2
** 7 then
2337 T
:= Standard_Integer_8
;
2339 elsif V
< Uint_2
** 15 then
2340 T
:= Standard_Integer_16
;
2342 elsif V
< Uint_2
** 31 then
2343 T
:= Standard_Integer_32
;
2345 elsif V
< Uint_2
** 63 then
2346 T
:= Standard_Integer_64
;
2353 L
:= Make_Integer_Literal
(Sloc
(N
), UI_Negate
(V
));
2355 L
:= Make_Integer_Literal
(Sloc
(N
), V
);
2358 -- Set type of result in case used elsewhere (see note at start)
2361 Set_Is_Static_Expression
(L
);
2363 -- We really need to set Analyzed here because we may be creating a
2364 -- very strange beast, namely an integer literal typed as fixed-point
2365 -- and the analyzer won't like that. Probably we should allow the
2366 -- Treat_Fixed_As_Integer flag to appear on integer literal nodes
2367 -- and teach the analyzer how to handle them ???
2371 end Integer_Literal
;
2377 function Real_Literal
(N
: Node_Id
; V
: Ureal
) return Node_Id
is
2381 L
:= Make_Real_Literal
(Sloc
(N
), V
);
2383 -- Set type of result in case used elsewhere (see note at start)
2385 Set_Etype
(L
, Universal_Real
);
2389 ------------------------
2390 -- Rounded_Result_Set --
2391 ------------------------
2393 function Rounded_Result_Set
(N
: Node_Id
) return Boolean is
2394 K
: constant Node_Kind
:= Nkind
(N
);
2396 if (K
= N_Type_Conversion
or else
2397 K
= N_Op_Divide
or else
2400 (Rounded_Result
(N
) or else Is_Integer_Type
(Etype
(N
)))
2406 end Rounded_Result_Set
;
2412 procedure Set_Result
2415 Rchk
: Boolean := False;
2416 Trunc
: Boolean := False)
2420 Expr_Type
: constant Entity_Id
:= Etype
(Expr
);
2421 Result_Type
: constant Entity_Id
:= Etype
(N
);
2424 -- No conversion required if types match and no range check or truncate
2426 if Result_Type
= Expr_Type
and then not (Rchk
or Trunc
) then
2429 -- Else perform required conversion
2432 Cnode
:= Build_Conversion
(N
, Result_Type
, Expr
, Rchk
, Trunc
);
2436 Analyze_And_Resolve
(N
, Result_Type
);