Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions xls/contrib/xlscc/translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,10 @@ absl::StatusOr<CValue> Translator::CreateInitValue(

if (initializer != nullptr) {
LValueModeGuard lvalue_mode(*this);
XLS_ASSIGN_OR_RETURN(bool lvalue_result, ctype->ContainsLValues(*this));
if (!lvalue_result) {
context().lvalue_mode = false;
}

XLS_ASSIGN_OR_RETURN(CValue cv, GenerateIR_Expr(initializer, loc));

Expand Down Expand Up @@ -4830,7 +4834,15 @@ absl::StatusOr<CValue> Translator::GenerateIR_Expr(const clang::Expr* expr,
// Implicit dereference
if (arr_val.type()->Is<CPointerType>()) {
XLSCC_CHECK_NE(arr_val.lvalue(), nullptr, loc);
CValue arr_val_orig = arr_val;

XLS_ASSIGN_OR_RETURN(arr_val, GenerateIR_Expr(arr_val.lvalue(), loc));

if (!arr_val.type()->Is<CArrayType>()) {
return absl::UnimplementedError(ErrorMessage(
loc, "Dereference didn't yield an array type, but %s from %s",
std::string(*arr_val.type()), std::string(*arr_val_orig.type())));
}
}

XLS_ASSIGN_OR_RETURN(CValue index_bval,
Expand Down
13 changes: 12 additions & 1 deletion xls/contrib/xlscc/unit_tests/translator_pointer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ TEST_F(TranslatorPointerTest, ToArraySubscript) {
const std::string content = R"(
int my_package() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int*a = &arr[10];
int*a = &arr[9];
return a[0];
})";
Run({}, 10, content);
}

TEST_F(TranslatorPointerTest, ToArraySubscript2) {
const std::string content = R"(
int my_package() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int*a = &arr[9];
int b = a[0];
return b;
})";
Run({}, 10, content);
}

TEST_F(TranslatorPointerTest, AssignToPointer) {
const std::string content = R"(
int my_package() {
Expand Down