diff --git a/oi/CodeGen.cpp b/oi/CodeGen.cpp index eda49a0..9505841 100644 --- a/oi/CodeGen.cpp +++ b/oi/CodeGen.cpp @@ -105,48 +105,6 @@ struct OIArray { )"; } -void defineJitLog(FeatureSet features, std::string& code) { - if (features[Feature::JitLogging]) { - code += R"( -extern int logFile; - -void __jlogptr(uintptr_t ptr) { - static constexpr char hexdigits[] = "0123456789abcdef"; - static constexpr size_t ptrlen = 2 * sizeof(ptr); - - static char hexstr[ptrlen + 1] = {}; - - size_t i = ptrlen; - while (i--) { - hexstr[i] = hexdigits[ptr & 0xf]; - ptr = ptr >> 4; - } - hexstr[ptrlen] = '\n'; - write(logFile, hexstr, sizeof(hexstr)); -} - -#define JLOG(str) \ - do { \ - if (__builtin_expect(logFile, 0)) { \ - write(logFile, str, sizeof(str) - 1); \ - } \ - } while (false) - -#define JLOGPTR(ptr) \ - do { \ - if (__builtin_expect(logFile, 0)) { \ - __jlogptr((uintptr_t)ptr); \ - } \ - } while (false) -)"; - } else { - code += R"( -#define JLOG(str) -#define JLOGPTR(ptr) -)"; - } -} - void addIncludes(const TypeGraph& typeGraph, FeatureSet features, std::string& code) { @@ -1137,7 +1095,7 @@ void CodeGen::generate( } addIncludes(typeGraph, config_.features, code); defineArray(code); - defineJitLog(config_.features, code); + FuncGen::DefineJitLog(code, config_.features); if (config_.features[Feature::TypedDataSegment]) { if (config_.features[Feature::Library]) { diff --git a/oi/FuncGen.cpp b/oi/FuncGen.cpp index f299bc1..85045fb 100644 --- a/oi/FuncGen.cpp +++ b/oi/FuncGen.cpp @@ -157,6 +157,48 @@ extern uintptr_t cookieValue; code.append(vars); } +void FuncGen::DefineJitLog(std::string& code, FeatureSet features) { + if (features[Feature::JitLogging]) { + code += R"( +extern int logFile; + +void __jlogptr(uintptr_t ptr) { + static constexpr char hexdigits[] = "0123456789abcdef"; + static constexpr size_t ptrlen = 2 * sizeof(ptr); + + static char hexstr[ptrlen + 1] = {}; + + size_t i = ptrlen; + while (i--) { + hexstr[i] = hexdigits[ptr & 0xf]; + ptr = ptr >> 4; + } + hexstr[ptrlen] = '\n'; + write(logFile, hexstr, sizeof(hexstr)); +} + +#define JLOG(str) \ + do { \ + if (__builtin_expect(logFile, 0)) { \ + write(logFile, str, sizeof(str) - 1); \ + } \ + } while (false) + +#define JLOGPTR(ptr) \ + do { \ + if (__builtin_expect(logFile, 0)) { \ + __jlogptr((uintptr_t)ptr); \ + } \ + } while (false) +)"; + } else { + code += R"( +#define JLOG(str) +#define JLOGPTR(ptr) +)"; + } +} + void FuncGen::DeclareStoreData(std::string& testCode) { testCode.append("void StoreData(uintptr_t data, size_t& dataSegOffset);\n"); } diff --git a/oi/FuncGen.h b/oi/FuncGen.h index 631a316..19eb4f6 100644 --- a/oi/FuncGen.h +++ b/oi/FuncGen.h @@ -30,6 +30,7 @@ namespace oi::detail { class FuncGen { public: static void DeclareExterns(std::string& code); + static void DefineJitLog(std::string& code, FeatureSet features); static void DeclareStoreData(std::string& testCode); static void DefineStoreData(std::string& testCode); diff --git a/oi/OICodeGen.cpp b/oi/OICodeGen.cpp index c924734..baffd70 100644 --- a/oi/OICodeGen.cpp +++ b/oi/OICodeGen.cpp @@ -3031,29 +3031,6 @@ bool OICodeGen::generateJitCode(std::string& code) { #define SAVE_SIZE(val) #define SAVE_DATA(val) StoreData(val, returnArg) )"); - - if (config.features[Feature::JitLogging]) { - code.append(R"( - #define JLOG(str) \ - do { \ - if (__builtin_expect(logFile, 0)) { \ - write(logFile, str, sizeof(str) - 1); \ - } \ - } while (false) - - #define JLOGPTR(ptr) \ - do { \ - if (__builtin_expect(logFile, 0)) { \ - __jlogptr((uintptr_t)ptr); \ - } \ - } while (false) - )"); - } else { - code.append(R"( - #define JLOG(str) - #define JLOGPTR(ptr) - )"); - } } else { code.append(R"( #define SAVE_SIZE(val) AddData(val, returnArg) @@ -3063,6 +3040,8 @@ bool OICodeGen::generateJitCode(std::string& code) { )"); } + FuncGen::DefineJitLog(code, config.features); + // The purpose of the anonymous namespace within `OIInternal` is that // anything defined within an anonymous namespace has internal-linkage, // and therefore won't appear in the symbol table of the resulting object