Skip to content

Commit

Permalink
Add __builtin_is_constant_evaluated
Browse files Browse the repository at this point in the history
  • Loading branch information
i-garrison authored and jonahgraham committed Apr 4, 2023
1 parent 3fdea6e commit f768aed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13888,4 +13888,10 @@ public void testArithmeticEvaluationOfRelationalOps() throws Exception {
BindingAssertionHelper helper = getAssertionHelper();
helper.assertVariableValue("shiftpack", 3);
}

// constexpr auto true_value = __builtin_is_constant_evaluated();
public void testBuiltinIsConstantEvaluated() throws Exception {
BindingAssertionHelper helper = getAssertionHelper();
helper.assertVariableValue("true_value", 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ private void addGnuBuiltins() {
ICPPExecution builtinClz = new ExecBuiltin(ExecBuiltin.BUILTIN_CLZ);
ICPPExecution builtinClzl = new ExecBuiltin(ExecBuiltin.BUILTIN_CLZL);
ICPPExecution builtinClzll = new ExecBuiltin(ExecBuiltin.BUILTIN_CLZLL);
ICPPExecution builtinIsConstantEvaluated = new ExecBuiltin(ExecBuiltin.BUILTIN_IS_CONSTANT_EVALUATED);

// Other Builtins (https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html) [incomplete]
function("void", "__builtin_abort");
Expand Down Expand Up @@ -358,6 +359,9 @@ private void addGnuBuiltins() {
function("_Decimal128", "__builtin_infd128");
function("float", "__builtin_inff");
function("long double", "__builtin_infl");
if (fCpp) {
function("bool", "__builtin_is_constant_evaluated", builtinIsConstantEvaluated);
}
function("int", "__builtin_isinf_sign", "...");
function("bool", "__builtin_isfinite", "double");
function("bool", "__builtin_isgreater", "float", "float");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ExecBuiltin implements ICPPExecution {
BUILTIN_CTZLL = 5, BUILTIN_POPCOUNT = 6, BUILTIN_POPCOUNTL = 7, BUILTIN_POPCOUNTLL = 8, BUILTIN_PARITY = 9,
BUILTIN_PARITYL = 10, BUILTIN_PARITYLL = 11, BUILTIN_ABS = 12, BUILTIN_LABS = 13, BUILTIN_LLABS = 14,
BUILTIN_CLRSB = 15, BUILTIN_CLRSBL = 16, BUILTIN_CLRSBLL = 17, BUILTIN_CLZ = 18, BUILTIN_CLZL = 19,
BUILTIN_CLZLL = 20;
BUILTIN_CLZLL = 20, BUILTIN_IS_CONSTANT_EVALUATED = 21;

private static IType intType = new CPPBasicType(Kind.eInt, 0);
private static IType longType = new CPPBasicType(Kind.eInt, CPPBasicType.IS_LONG);
Expand Down Expand Up @@ -100,6 +100,8 @@ public ICPPExecution executeForFunctionCall(ActivationRecord record, ConstexprEv
return executeBuiltinClz(record, context, longType);
case BUILTIN_CLZLL:
return executeBuiltinClz(record, context, longlongType);
case BUILTIN_IS_CONSTANT_EVALUATED:
return executeBuiltinIsConstantEvaluated(record, context, null);
}
return null;
}
Expand Down Expand Up @@ -264,6 +266,12 @@ private ICPPExecution executeBuiltinClz(ActivationRecord record, ConstexprEvalua
return new ExecReturn(new EvalFixed(intType, ValueCategory.PRVALUE, IntegralValue.create(result)));
}

private ICPPExecution executeBuiltinIsConstantEvaluated(ActivationRecord record, ConstexprEvaluationContext context,
IType argType) {
// Since this is only evaluated in constexpr evaluation context, return true
return new ExecReturn(new EvalFixed(intType, ValueCategory.PRVALUE, IntegralValue.create(1)));
}

@Override
public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
buffer.putShort(ITypeMarshalBuffer.EXEC_BUILTIN);
Expand Down

0 comments on commit f768aed

Please sign in to comment.