From 897c4382affbe67052db5d15798588a85e370b7b Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Sun, 16 Jun 2013 01:02:46 +0200 Subject: [PATCH] Do not re-codegen struct literals on taking address of globals. The AST the frontend generates is arguably invalid, but we have to deal with that. Fixes DMD testcase 'interpret'. --- gen/toir.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gen/toir.cpp b/gen/toir.cpp index 7bd2b511..32395461 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1231,6 +1231,24 @@ DValue* AddrExp::toElem(IRState* p) { IF_LOG Logger::println("AddrExp::toElem: %s @ %s", toChars(), type->toChars()); LOG_SCOPE; + + // The address of a StructLiteralExp can in fact be a global variable, check + // for that instead of re-codegening the literal. + if (e1->op == TOKstructliteral) + { + IF_LOG Logger::println("is struct literal"); + StructLiteralExp* se = static_cast(e1); + + // DMD uses origin here as well, necessary to handle messed-up AST on + // forward references. + if (se->origin->globalVar) + { + IF_LOG Logger::cout() << "returning address of global: " << + *se->globalVar << '\n'; + return new DImValue(type, DtoBitCast(se->origin->globalVar, DtoType(type))); + } + } + DValue* v = e1->toElem(p); if (v->isField()) { Logger::println("is field");