--- a/src/Declaration.cpp +++ b/src/Declaration.cpp @@ -15,6 +15,7 @@ along with this program. If not, see . */ +#include "VCCDebug.h" #include "Declaration.h" #include "TranslationUnit.h" @@ -334,6 +335,8 @@ bool canBeStaticInit = (initializationExpr && initializationExpr->isStaticallyInitializable(*getTypeDesc())); if (canBeStaticInit != withStaticInitializer) return true; // nothing to do + + if (isGlobal()) VCCDebug::variable(out, *this, "", ""); if (withStaticInitializer) { --- a/src/ForStmt.cpp +++ b/src/ForStmt.cpp @@ -15,6 +15,7 @@ along with this program. If not, see . */ +#include "VCCDebug.h" #include "ForStmt.h" #include "TranslationUnit.h" @@ -115,6 +116,8 @@ //cerr << "pushing ForStmt scope:\n"; pushScopeIfExists(); + const string debugBegin = getScope() && VCCDebug::enabled() ? VCCDebug::label() : "", debugEnd = getScope() && VCCDebug::enabled() ? VCCDebug::label() : ""; + VCCDebug::scope(out, getScope(), debugBegin, debugEnd); TranslationUnit &tu = TranslationUnit::instance(); string bodyLabel = TranslationUnit::instance().generateLabel('L'); @@ -128,6 +131,7 @@ tu.popBreakableLabels(); //cerr << "popping ForStmt scope:\n"; + if (getScope() && VCCDebug::enabled()) out.emitLabel(debugEnd); popScopeIfExists(); return cs; } --- a/src/FunctionDef.cpp +++ b/src/FunctionDef.cpp @@ -15,6 +15,7 @@ along with this program. If not, see . */ +#include "VCCDebug.h" #include "FunctionDef.h" #include "Scope.h" @@ -853,11 +854,23 @@ functor.emitComments(out); } + // VCC Plus: exact compiler-resolved body/scope boundaries, before epilogue. + const string debugBody = VCCDebug::enabled() ? VCCDebug::label() : "", debugEnd = VCCDebug::enabled() ? VCCDebug::label() : ""; + if (VCCDebug::enabled()) out.emitComment("VCCDBG1 {\"kind\":\"function\",\"name\":" + VCCDebug::quote(functionId) + + ",\"location\":" + VCCDebug::quote(getLineNo()) + + ",\"symbol\":" + VCCDebug::quote(functionLabel) + + ",\"begin\":" + VCCDebug::quote(debugBody) + ",\"end\":" + VCCDebug::quote(debugEnd) + + ",\"frame\":" + (needStackFrame && !isAssemblyOnly() && callConvention == DEFAULT_CMOC_CALL_CONV + && TranslationUnit::instance().getFramePointerOption() == FramePointerOption::KEEP_ALL + && TranslationUnit::instance().hasUnoptimizedDebugStorage() ? "true" : "false") + "}"); + // Generate code for the body: TranslationUnit::instance().setCurrentFunctionEndLabel(endLabel); TranslationUnit::instance().pushScope(const_cast(getScope())); // const_cast should be removed... + VCCDebug::scope(out, getScope(), debugBody, debugEnd); bool success = bodyStmts->emitCode(out, false); + if (VCCDebug::enabled()) out.emitLabel(debugEnd); out.emitLabel(endLabel, "end of " + functionId + "()"); if (!asmOnly && callConvention == GCC6809_CALL_CONV && getTypeSize() == 2) --- a/src/TranslationUnit.cpp +++ b/src/TranslationUnit.cpp @@ -2573,3 +2573,6 @@ irFile << "}\n"; // main JSON object return true; } + +// VCC Plus: optimized stores/register reuse cannot claim stable C locals. +bool TranslationUnit::hasUnoptimizedDebugStorage() const { return params.optimizationLevel == 0; } --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -194,6 +194,7 @@ bool isRelocatabilitySupported() const; FramePointerOption getFramePointerOption() const; + bool hasUnoptimizedDebugStorage() const; // Called during parsing, right after the parser recognized the #pragma directive. // May change some fields of this TranslationUnit object. --- a/src/TreeSequence.cpp +++ b/src/TreeSequence.cpp @@ -15,6 +15,7 @@ along with this program. If not, see . */ +#include "VCCDebug.h" #include "TreeSequence.h" #include "TranslationUnit.h" @@ -141,6 +142,8 @@ const bool isCommaExpr = !!dynamic_cast(this); pushScopeIfExists(); + const string debugBegin = getScope() && VCCDebug::enabled() ? VCCDebug::label() : "", debugEnd = getScope() && VCCDebug::enabled() ? VCCDebug::label() : ""; + VCCDebug::scope(out, getScope(), debugBegin, debugEnd); bool success = true; for (vector::const_iterator it = sequence.begin(); it != sequence.end(); it++) @@ -178,6 +181,7 @@ } } + if (getScope() && VCCDebug::enabled()) out.emitLabel(debugEnd); popScopeIfExists(); return success; --- /dev/null +++ b/src/VCCDebug.h @@ -0,0 +1,101 @@ +// VCC Plus compiler debug extension. SPDX-License-Identifier: GPL-3.0-or-later +// Metadata comments and labels occupy no guest memory. Never infer C storage +// or type layout from source text: use the compiler's resolved declarations. +#ifndef VCC_DEBUG_H +#define VCC_DEBUG_H +#include "ASMText.h" +#include "Declaration.h" +#include "Scope.h" +#include "TranslationUnit.h" +#include "ClassDef.h" +#include +#include +namespace VCCDebug { +inline bool enabled() { return TranslationUnit::instance().getFramePointerOption() == FramePointerOption::KEEP_ALL; } +inline std::string arrayType(const TypeDesc *, const std::vector &, unsigned); +inline std::string quote(const std::string &s) { + std::ostringstream o; o << '"'; + for (unsigned char c : s) { + if (c == '"' || c == '\\') o << '\\' << c; + else if (c < 32) o << "\\u00" << std::hex << std::setw(2) << std::setfill('0') << unsigned(c) << std::dec; + else o << c; + } + o << '"'; return o.str(); +} +inline std::string type(const TypeDesc *td, unsigned depth = 0) { + std::ostringstream o; + const int size = td->type == FUNCTION_TYPE || td->type == VOID_TYPE || (td->type == ARRAY_TYPE && td->numArrayElements == TypeDesc::NO_DIMENSION) ? 0 : TranslationUnit::instance().getTypeSize(*td); + o << "{\"name\":" << quote(td->toString()) << ",\"size\":" << (size < 0 ? 0 : size); + if (depth > 5) o << ",\"kind\":\"unsupported\""; + else if (td->type == BYTE_TYPE || td->type == WORD_TYPE || td->isLong()) + o << ",\"kind\":\"integer\",\"signed\":" << (td->isSigned ? "true" : "false"); + else if (td->isReal()) { + auto &tu = TranslationUnit::instance(); + const char *format = "unsupported"; + if (!td->isDouble() && tu.isFloatingPointSupported()) { + if (tu.getFloatingPointLibrary() == FloatingPointLibrary::MC6839_LIB && size == 4) + format = "ieee754-binary32-be"; + else if (size == 5 && (tu.getFloatingPointLibrary() == FloatingPointLibrary::NATIVE_LIB + || tu.getTargetPlatform() == COCO_BASIC || tu.getTargetPlatform() == DRAGON)) + format = "coco-basic-40"; + } + o << ",\"kind\":\"floating\",\"floatingFormat\":" << quote(format); + } + else if (td->type == POINTER_TYPE) + o << ",\"kind\":\"pointer\",\"element\":" << type(td->pointedTypeDesc, depth + 1); + else if (td->type == ARRAY_TYPE && td->numArrayElements != TypeDesc::NO_DIMENSION) + o << ",\"kind\":\"array\",\"count\":" << td->numArrayElements << ",\"element\":" << type(td->pointedTypeDesc, depth + 1); + else if (td->type == CLASS_TYPE) { + const ClassDef *cl = TranslationUnit::instance().getClassDef(td->className); + o << ",\"kind\":\"aggregate\",\"members\":["; + if (cl) for (size_t i=0;igetNumDataMembers();++i) { + auto m = cl->getDataMember(i); + if (i) o << ','; + auto dims = m->getArrayDimensions(); m->getTypeDesc()->appendDimensions(dims); + o << "{\"name\":" << quote(m->getName()) << ",\"offset\":" << cl->getDataMemberOffset(i, NULL) + << ",\"type\":" << (dims.empty() ? type(m->getTypeDesc(), depth+1) : arrayType(m->getTypeDesc(), dims, depth+1)) << '}'; + } + o << ']'; + } else o << ",\"kind\":\"unsupported\""; + o << '}'; return o.str(); +} +inline std::string arrayType(const TypeDesc *original, const std::vector &dims, unsigned depth) { + const TypeDesc *td=original; + while (td->type == ARRAY_TYPE) td=td->pointedTypeDesc; + std::string result=type(td, depth+1); + size_t size=TranslationUnit::instance().getTypeSize(*td); + for (auto it=dims.rbegin();it!=dims.rend();++it) { + size *= *it; + std::ostringstream o; o << "{\"name\":" << quote(original->toString()) + << ",\"kind\":\"array\",\"size\":" << size << ",\"count\":" << *it << ",\"element\":" << result << '}'; + result=o.str(); + } + return result; +} +inline std::string declarationType(const Declaration &decl) { + return decl.getTypeDesc()->type == ARRAY_TYPE ? arrayType(decl.getTypeDesc(), decl.arrayDimensions, 0) : type(decl.getTypeDesc()); +} +inline std::string label() { return ".vcc.debug." + TranslationUnit::instance().generateLabel('L'); } +inline void variable(ASMText &out, const Declaration &d, const std::string &begin, const std::string &end) { + if (!enabled() || d.isExtern || d.getVariableId().empty() || d.getVariableId()[0] == '$') return; + uint16_t size=0; if (!d.getVariableSizeInBytes(size) || !size) return; + std::ostringstream o; o << "VCCDBG1 {\"kind\":\"variable\",\"name\":" << quote(d.getVariableId()) + << ",\"location\":" << quote(d.getLineNo()) << ",\"type\":" << declarationType(d) + << ",\"begin\":" << quote(begin) << ",\"end\":" << quote(end) + << ",\"static\":" << (d.isStatic() ? "true" : "false"); + if (d.isGlobal() || d.isLocalStatic()) o << ",\"symbol\":" << quote(d.getLabel()); + else o << ",\"offset\":" << d.getFrameDisplacement(); + o << '}'; out.emitComment(o.str()); +} +inline void scope(ASMText &out, const Scope *s, const std::string &begin, const std::string &end) { + if (!enabled() || !s) return; + class Visitor : public Scope::DeclarationFunctor { + ASMText &out; const std::string &begin, &end; + public: + Visitor(ASMText &o, const std::string &b, const std::string &e):out(o),begin(b),end(e){} + bool operator()(Declaration &d) override { variable(out,d,begin,end); return true; } + } visit(out,begin,end); + out.emitLabel(begin); s->iterateDeclarations(visit,false); +} +} +#endif