@@ -55,25 +55,35 @@ public ICPPExecution executeForFunctionCall(ActivationRecord record, ConstexprEv
5555
5656 switch (funcId ) {
5757 case BUILTIN_FFS :
58+ return executeBuiltinFfs (record , context , intType );
5859 case BUILTIN_FFSL :
60+ return executeBuiltinFfs (record , context , longType );
5961 case BUILTIN_FFSLL :
60- return executeBuiltinFfs (record , context );
62+ return executeBuiltinFfs (record , context , longlongType );
6163 case BUILTIN_CTZ :
64+ return executeBuiltinCtz (record , context , intType );
6265 case BUILTIN_CTZL :
66+ return executeBuiltinCtz (record , context , longType );
6367 case BUILTIN_CTZLL :
64- return executeBuiltinCtz (record , context );
68+ return executeBuiltinCtz (record , context , longlongType );
6569 case BUILTIN_POPCOUNT :
70+ return executeBuiltinPopcount (record , context , intType );
6671 case BUILTIN_POPCOUNTL :
72+ return executeBuiltinPopcount (record , context , longType );
6773 case BUILTIN_POPCOUNTLL :
68- return executeBuiltinPopcount (record , context );
74+ return executeBuiltinPopcount (record , context , longlongType );
6975 case BUILTIN_PARITY :
76+ return executeBuiltinParity (record , context , intType );
7077 case BUILTIN_PARITYL :
78+ return executeBuiltinParity (record , context , longType );
7179 case BUILTIN_PARITYLL :
72- return executeBuiltinParity (record , context );
80+ return executeBuiltinParity (record , context , longlongType );
7381 case BUILTIN_ABS :
82+ return executeBuiltinAbs (record , context , intType );
7483 case BUILTIN_LABS :
84+ return executeBuiltinAbs (record , context , longType );
7585 case BUILTIN_LLABS :
76- return executeBuiltinAbs (record , context );
86+ return executeBuiltinAbs (record , context , longlongType );
7787 }
7888 return null ;
7989 }
@@ -82,16 +92,18 @@ public ICPPExecution executeForFunctionCall(ActivationRecord record, ConstexprEv
8292 * Return an execution representing __builtin_ffs or __builtin_ctz
8393 */
8494 private ICPPExecution executeBuiltinFfsCtz (ActivationRecord record , ConstexprEvaluationContext context ,
85- boolean isCtz ) {
95+ boolean isCtz , IType argType ) {
8696 ICPPEvaluation arg0 = record .getVariable (new CPPBuiltinParameter (null , 0 ));
8797
8898 IValue argValue = arg0 .getValue ();
89- if (!(argValue instanceof IntegralValue ))
99+ Number numberVal = argValue .numberValue ();
100+ numberVal = Conversions .narrowNumberValue (numberVal , argType );
101+ if (numberVal == null )
90102 return null ;
91103
92104 // __builtin_ffs returns 0 if arg is 0, or 1+count where count is the number of trailing 0 bits
93105 // __builtin_ctz is undefined if arg is 0, or returns count
94- long arg = argValue . numberValue () .longValue ();
106+ long arg = numberVal .longValue ();
95107 if (arg == 0 ) {
96108 if (isCtz ) {
97109 return null ;
@@ -108,26 +120,30 @@ private ICPPExecution executeBuiltinFfsCtz(ActivationRecord record, ConstexprEva
108120 return new ExecReturn (new EvalFixed (intType , ValueCategory .PRVALUE , IntegralValue .create (count + increment )));
109121 }
110122
111- private ICPPExecution executeBuiltinFfs (ActivationRecord record , ConstexprEvaluationContext context ) {
112- return executeBuiltinFfsCtz (record , context , false /* ffs */ );
123+ private ICPPExecution executeBuiltinFfs (ActivationRecord record , ConstexprEvaluationContext context ,
124+ IType argType ) {
125+ return executeBuiltinFfsCtz (record , context , false /* ffs */ , argType );
113126 }
114127
115- private ICPPExecution executeBuiltinCtz (ActivationRecord record , ConstexprEvaluationContext context ) {
116- return executeBuiltinFfsCtz (record , context , true /* ctz */ );
128+ private ICPPExecution executeBuiltinCtz (ActivationRecord record , ConstexprEvaluationContext context ,
129+ IType argType ) {
130+ return executeBuiltinFfsCtz (record , context , true /* ctz */ , argType );
117131 }
118132
119133 /*
120134 * Return an execution representing __builtin_popcount
121135 */
122136 private ICPPExecution executeBuiltinPopcountParity (ActivationRecord record , ConstexprEvaluationContext context ,
123- boolean isParity ) {
137+ boolean isParity , IType argType ) {
124138 ICPPEvaluation arg0 = record .getVariable (new CPPBuiltinParameter (null , 0 ));
125139
126140 IValue argValue = arg0 .getValue ();
127- if (!(argValue instanceof IntegralValue ))
141+ Number numberVal = argValue .numberValue ();
142+ numberVal = Conversions .narrowNumberValue (numberVal , argType );
143+ if (numberVal == null )
128144 return null ;
129145
130- long arg = argValue . numberValue () .longValue ();
146+ long arg = numberVal .longValue ();
131147 int count = 0 ;
132148 while (arg != 0 ) {
133149 if ((arg & 1 ) != 0 )
@@ -140,38 +156,30 @@ private ICPPExecution executeBuiltinPopcountParity(ActivationRecord record, Cons
140156 return new ExecReturn (new EvalFixed (intType , ValueCategory .PRVALUE , IntegralValue .create (count )));
141157 }
142158
143- private ICPPExecution executeBuiltinPopcount (ActivationRecord record , ConstexprEvaluationContext context ) {
144- return executeBuiltinPopcountParity (record , context , false );
159+ private ICPPExecution executeBuiltinPopcount (ActivationRecord record , ConstexprEvaluationContext context ,
160+ IType argType ) {
161+ return executeBuiltinPopcountParity (record , context , false , argType );
145162 }
146163
147- private ICPPExecution executeBuiltinParity (ActivationRecord record , ConstexprEvaluationContext context ) {
148- return executeBuiltinPopcountParity (record , context , true );
164+ private ICPPExecution executeBuiltinParity (ActivationRecord record , ConstexprEvaluationContext context ,
165+ IType argType ) {
166+ return executeBuiltinPopcountParity (record , context , true , argType );
149167 }
150168
151- private ICPPExecution executeBuiltinAbs (ActivationRecord record , ConstexprEvaluationContext context ) {
169+ private ICPPExecution executeBuiltinAbs (ActivationRecord record , ConstexprEvaluationContext context ,
170+ IType argType ) {
152171 ICPPEvaluation arg0 = record .getVariable (new CPPBuiltinParameter (null , 0 ));
153172
154173 IValue argValue = arg0 .getValue ();
155- if (!(argValue instanceof IntegralValue ))
174+ Number argNumber = argValue .numberValue ();
175+ argNumber = Conversions .narrowNumberValue (argNumber , argType );
176+ if (argNumber == null )
156177 return null ;
157178
158- long arg = argValue . numberValue () .longValue ();
179+ long arg = argNumber .longValue ();
159180 long result = Math .abs (arg );
160181
161- IType resultType = null ;
162- switch (funcId ) {
163- case BUILTIN_ABS :
164- resultType = intType ;
165- break ;
166- case BUILTIN_LABS :
167- resultType = longType ;
168- break ;
169- case BUILTIN_LLABS :
170- resultType = longlongType ;
171- break ;
172- }
173-
174- return new ExecReturn (new EvalFixed (resultType , ValueCategory .PRVALUE , IntegralValue .create (result )));
182+ return new ExecReturn (new EvalFixed (argType , ValueCategory .PRVALUE , IntegralValue .create (result )));
175183 }
176184
177185 @ Override
0 commit comments