From 78b5513f78e53634ffdad056eae46428c172e566 Mon Sep 17 00:00:00 2001 From: Yuri Iozzelli Date: Fri, 23 Feb 2024 10:22:51 +0100 Subject: [PATCH 1/3] support disablig AS and attribute injection via #pragma cheerp env none --- clang/include/clang/Sema/Sema.h | 2 +- clang/lib/Sema/SemaAttr.cpp | 8 +++++++- clang/lib/Sema/SemaCheerp.cpp | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index bd3d431831dc..86fc26c631a2 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -404,7 +404,7 @@ class Sema final { OpenCLOptions OpenCLFeatures; FPOptions CurFPFeatures; - LangOptions::CheerpDefaultEnvMode CurCheerpEnv; + LangAS CurCheerpFallbackAS; const LangOptions &LangOpts; Preprocessor &PP; diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp index aadb5b54bcd1..7d9c137be3d0 100644 --- a/clang/lib/Sema/SemaAttr.cpp +++ b/clang/lib/Sema/SemaAttr.cpp @@ -1163,7 +1163,13 @@ void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) { } void Sema::ActOnPragmaCheerpEnv(LangOptions::CheerpDefaultEnvMode Mode, SourceLocation PragmaLoc) { - CurCheerpEnv = Mode; + if (Mode == LangOptions::CheerpDefaultEnvMode::GenericJS) { + CurCheerpFallbackAS = LangAS::cheerp_genericjs; + } else if (Mode == LangOptions::CheerpDefaultEnvMode::Wasm) { + CurCheerpFallbackAS = LangAS::cheerp_wasm; + } else { + CurCheerpFallbackAS = LangAS::Default; + } } void Sema::ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn) { diff --git a/clang/lib/Sema/SemaCheerp.cpp b/clang/lib/Sema/SemaCheerp.cpp index 993181b89fa6..6c35b0609899 100644 --- a/clang/lib/Sema/SemaCheerp.cpp +++ b/clang/lib/Sema/SemaCheerp.cpp @@ -804,9 +804,9 @@ cheerp::CheerpAttributeToAdd cheerp::getCheerpAttributeToAdd(clang::Sema& S, con } // Or set default attr based on default - if (S.CurCheerpEnv == clang::LangOptions::Wasm) + if (S.CurCheerpFallbackAS == clang::LangAS::cheerp_wasm) return CheerpAttributeToAdd::AsmJSLike; - else if (S.CurCheerpEnv == clang::LangOptions::GenericJS) + else if (S.CurCheerpFallbackAS == clang::LangAS::cheerp_genericjs) return CheerpAttributeToAdd::GenericJS; else return CheerpAttributeToAdd::None; From 480f74d5c5a4481be61e8bcc6d1de9d76e7ebd97 Mon Sep 17 00:00:00 2001 From: Yuri Iozzelli Date: Tue, 28 Jan 2025 15:07:43 +0100 Subject: [PATCH 2/3] libcxx: cast `free` for indirect usage --- libcxx/include/locale | 8 ++++++++ libcxx/src/ios.cpp | 13 +++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/libcxx/include/locale b/libcxx/include/locale index 18a3757a8f58..b99629d8bc86 100644 --- a/libcxx/include/locale +++ b/libcxx/include/locale @@ -211,6 +211,10 @@ template class messages_byname; #include #include +#ifdef __CHEERP__ +#define free reinterpret_cast(free) +#endif + #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) // Most unix variants have catopen. These are the specific ones that don't. # if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__) @@ -4375,4 +4379,8 @@ _LIBCPP_POP_MACROS # include #endif +#ifdef __CHEERP__ +#undef free +#endif + #endif // _LIBCPP_LOCALE diff --git a/libcxx/src/ios.cpp b/libcxx/src/ios.cpp index 870406b554d9..5d0af60c28bc 100644 --- a/libcxx/src/ios.cpp +++ b/libcxx/src/ios.cpp @@ -279,15 +279,20 @@ ios_base::init(void* sb) ::new(&__loc_) locale; } +#ifdef __CHEERP__ +#define indirectfree reinterpret_cast(free) +#else +#define indirectfree free +#endif void ios_base::copyfmt(const ios_base& rhs) { // If we can't acquire the needed resources, throw bad_alloc (can't set badbit) // Don't alter *this until all needed resources are acquired - unique_ptr new_callbacks(0, free); - unique_ptr new_ints(0, free); - unique_ptr new_longs(0, free); - unique_ptr new_pointers(0, free); + unique_ptr new_callbacks(0, indirectfree); + unique_ptr new_ints(0, indirectfree); + unique_ptr new_longs(0, indirectfree); + unique_ptr new_pointers(0, indirectfree); if (__event_cap_ < rhs.__event_size_) { size_t newesize = sizeof(event_callback) * rhs.__event_size_; From e41267ad1f3201f70d95381c16e99ffee0cab0fa Mon Sep 17 00:00:00 2001 From: Yuri Iozzelli Date: Thu, 9 Jan 2025 14:03:44 +0100 Subject: [PATCH 3/3] libcxx: Make _Rep_base bytelayout only in genericjs mode --- libcxx/include/stdexcept | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libcxx/include/stdexcept b/libcxx/include/stdexcept index d24b42b2b89b..910c5bc7c46e 100644 --- a/libcxx/include/stdexcept +++ b/libcxx/include/stdexcept @@ -54,7 +54,11 @@ public: _LIBCPP_BEGIN_NAMESPACE_STD typedef int count_t; -struct [[cheerp::bytelayout]] _Rep_base { +struct +#if defined(__CHEERP__) && !defined(__ASMJS__) +[[cheerp::bytelayout]] +#endif +_Rep_base { std::size_t len; std::size_t cap; count_t count;