diff --git a/Compiler/AST/ASTBinaryOperator_CG.cpp b/Compiler/AST/ASTBinaryOperator_CG.cpp index 7cb6e03..935a947 100644 --- a/Compiler/AST/ASTBinaryOperator_CG.cpp +++ b/Compiler/AST/ASTBinaryOperator_CG.cpp @@ -17,6 +17,21 @@ Value* ASTBinaryOperator::generate(FunctionCodeGenerator *fg) const { return generateLogical(fg); } + // The 🤷‍♀️ operand is never complied (its type stays noReturn), so it must not be generated: + // building an optional of void is undefined behaviour inside LLVM and crashes on wasm32. + if (builtIn_ == BuiltInType::IsNoValueLeft) { + auto left = left_->generate(fg); + return left_->expressionType().storageType() == StorageType::Box + ? fg->buildHasNoValueBox(left) + : fg->buildOptionalHasNoValue(left, left_->expressionType()); + } + if (builtIn_ == BuiltInType::IsNoValueRight) { + auto right = right_->generate(fg); + return right_->expressionType().storageType() == StorageType::Box + ? fg->buildHasNoValueBox(right) + : fg->buildOptionalHasNoValue(right, right_->expressionType()); + } + if (builtIn_ != BuiltInType::None) { auto left = left_->generate(fg); auto right = right_->generate(fg); @@ -71,14 +86,6 @@ Value* ASTBinaryOperator::generate(FunctionCodeGenerator *fg) const { return fg->builder().CreateAnd(left, right); case BuiltInType::Equal: return fg->builder().CreateICmpEQ(left, right); - case BuiltInType::IsNoValueLeft: - return left_->expressionType().storageType() == StorageType::Box - ? fg->buildHasNoValueBox(left) - : fg->buildOptionalHasNoValue(left, left_->expressionType()); - case BuiltInType::IsNoValueRight: - return right_->expressionType().storageType() == StorageType::Box - ? fg->buildHasNoValueBox(right) - : fg->buildOptionalHasNoValue(right, right_->expressionType()); default: break; } diff --git a/Compiler/Compiler.cpp b/Compiler/Compiler.cpp index d905124..48bc732 100644 --- a/Compiler/Compiler.cpp +++ b/Compiler/Compiler.cpp @@ -93,7 +93,9 @@ void Compiler::LinkPhase::perform(Compiler *compiler) { auto runtimeLib = compiler->findBinaryPathPackage(compiler->searchPackage("runtime", SourcePosition()), "runtime"); cmd << " " << runtimeLib << " -o " << outPath_; +#ifndef __wasi__ system(cmd.str().c_str()); +#endif } void Compiler::ArchivePhase::perform(Compiler *compiler) { @@ -102,7 +104,9 @@ void Compiler::ArchivePhase::perform(Compiler *compiler) { cmd.append(outPath_); cmd.append(" "); cmd.append(objectFilePath_); +#ifndef __wasi__ system(cmd.c_str()); +#endif } std::string Compiler::searchPackage(const std::string &name, const SourcePosition &p) { diff --git a/Compiler/Generation/CodeGenerator.cpp b/Compiler/Generation/CodeGenerator.cpp index e9e8765..1b05138 100644 --- a/Compiler/Generation/CodeGenerator.cpp +++ b/Compiler/Generation/CodeGenerator.cpp @@ -1,3 +1,4 @@ +#include // // CodeGenerator.cpp // Emojicode @@ -50,7 +51,7 @@ CodeGenerator::CodeGenerator(Compiler *compiler, bool optimize) llvm::InitializeAllAsmParsers(); llvm::InitializeAllAsmPrinters(); - auto targetTriple = llvm::sys::getDefaultTargetTriple(); + std::string targetTriple = getenv("EMOJICODE_TARGET") ? getenv("EMOJICODE_TARGET") : llvm::sys::getDefaultTargetTriple(); std::string error; auto target = llvm::TargetRegistry::lookupTarget(targetTriple, error); diff --git a/Compiler/Generation/FunctionCodeGenerator.cpp b/Compiler/Generation/FunctionCodeGenerator.cpp index f721635..51dea65 100644 --- a/Compiler/Generation/FunctionCodeGenerator.cpp +++ b/Compiler/Generation/FunctionCodeGenerator.cpp @@ -469,6 +469,15 @@ void FunctionCodeGenerator::manageBox(bool retain, llvm::Value *boxInfo, llvm::V auto conf = builder().CreateBitCast(boxInfo, typeHelper().protocolConformance()->getPointerTo()); fnPtr = builder().CreateConstInBoundsGEP2_32(typeHelper().protocolConformance(), conf, 0, retain ? 3 : 4); } + else if (type.boxedFor().type() == TypeType::MultiProtocol) { + // A multiprotocol box does not store a box info but a pointer to an array of protocol conformances (one per + // protocol). They all belong to the boxed value's type, so the first one provides the right retain/release. + // Treating the array as a box info read past its end and called an arbitrary pointer. + auto confPtrType = typeHelper().protocolConformance()->getPointerTo(); + auto array = builder().CreateBitCast(boxInfo, confPtrType->getPointerTo()); + auto conf = builder().CreateLoad(confPtrType, array); + fnPtr = builder().CreateConstInBoundsGEP2_32(typeHelper().protocolConformance(), conf, 0, retain ? 3 : 4); + } else { fnPtr = builder().CreateConstInBoundsGEP2_32(typeHelper().boxInfo(), boxInfo, 0, retain ? 1 : 2); } diff --git a/Compiler/Generation/RunTimeHelper.cpp b/Compiler/Generation/RunTimeHelper.cpp index f171182..a5b5b42 100644 --- a/Compiler/Generation/RunTimeHelper.cpp +++ b/Compiler/Generation/RunTimeHelper.cpp @@ -111,7 +111,7 @@ void RunTimeHelper::declareRunTime() { buildRetainRelease(Type(Type::noReturn(), {}, Type::noReturn()), "callable.boxRetain", "callable.boxRelease", boxInfoCallables_); - malloc_ = declareRunTimeFunction("malloc", llvm::Type::getInt8PtrTy(generator_->context()), + malloc_ = declareRunTimeFunction("ejcMalloc", llvm::Type::getInt8PtrTy(generator_->context()), llvm::Type::getInt64Ty(generator_->context())); malloc_->removeFnAttr(llvm::Attribute::NoRecurse); malloc_->addAttribute(llvm::AttributeList::ReturnIndex, llvm::Attribute::NonNull); diff --git a/Compiler/Utils/rang.hpp b/Compiler/Utils/rang.hpp index 831eda4..eec9ba2 100644 --- a/Compiler/Utils/rang.hpp +++ b/Compiler/Utils/rang.hpp @@ -1,7 +1,7 @@ #ifndef RANG_DOT_HPP #define RANG_DOT_HPP -#if defined(__unix__) || defined(__unix) || defined(__linux__) +#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__wasi__) #define OS_LINUX #elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) #define OS_WIN diff --git a/files/File.cpp b/files/File.cpp index 9bad4dc..e833720 100644 --- a/files/File.cpp +++ b/files/File.cpp @@ -37,8 +37,10 @@ extern "C" File* filesFileNewReading(String *path, runtime::Raiser *raiser) { return file; } -extern "C" void filesFileWrite(File *file, Data *data) { +// files.🍇 declares ✏️ as raising, so the compiler passes a Raiser (a missing parameter is a signature mismatch on wasm). +extern "C" void filesFileWrite(File *file, Data *data, runtime::Raiser *raiser) { file->file_.write(reinterpret_cast(data->data.get()), data->count); + EJC_COND_RAISE_IO_VOID(!file->file_.fail(), raiser); } extern "C" void filesFileClose(File *file) { diff --git a/runtime/Runtime.h b/runtime/Runtime.h index 6ca41b2..66e3883 100644 --- a/runtime/Runtime.h +++ b/runtime/Runtime.h @@ -48,8 +48,16 @@ using is_complete = decltype(is_complete_impl(std::declval())); } // namespace util +/// Mirrors the compiler's %runTimeTypeInfo = { i16, i16, i8 } so that the layout +/// matches on every target (a plain pointer only happened to match on 64-bit). +struct ClassRunTimeTypeInfo { + uint16_t genericParameterCount; + uint16_t genericParameterOffset; + uint8_t flags; +}; + struct ClassInfo { - void *rtti; + ClassRunTimeTypeInfo rtti; void **dispatchTable; void *protocolTable; ClassInfo *superclass; diff --git a/runtime/main.cpp b/runtime/main.cpp index 1f37c62..983ae7d 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -26,6 +26,10 @@ runtime::internal::ControlBlock* runtime::internal::newControlBlock() { extern "C" runtime::Integer fn_1f3c1(); +extern "C" int8_t* ejcMalloc(runtime::Integer size) { + return static_cast(malloc(static_cast(size))); +} + extern "C" int8_t* ejcAlloc(runtime::Integer size) { auto ptr = malloc(size); *static_cast(ptr) = new runtime::internal::ControlBlock; diff --git a/s/String.cpp b/s/String.cpp index fd614e0..d1a17a0 100644 --- a/s/String.cpp +++ b/s/String.cpp @@ -123,7 +123,14 @@ extern "C" runtime::SimpleOptional sStringFindFromIndex(String return runtime::NoValue; } +#ifdef __wasi__ +extern "C" void sStringCodepoints(String *string, void *cbFn, void *cbCapture) { + runtime::Callable cb; + void *parts[2] = {cbFn, cbCapture}; + std::memcpy(&cb, parts, sizeof(parts)); +#else extern "C" void sStringCodepoints(String *string, runtime::Callable cb) { +#endif for (size_t off = 0; off < string->count;) { utf8proc_int32_t codepoint; auto state = utf8proc_iterate(reinterpret_cast(string->characters.get()) + off, @@ -140,32 +147,41 @@ extern "C" s::String* sStringTrim(String *string) { for (; begin < string->count;) { utf8proc_int32_t codepoint; auto state = utf8proc_iterate(reinterpret_cast(string->characters.get()) + begin, - string->count, &codepoint); + string->count - begin, &codepoint); if (state < 0) break; if (utf8proc_get_property(codepoint)->bidi_class != UTF8PROC_BIDI_CLASS_WS) break; begin += state; } - size_t end = begin - 1; + // end is exclusive: the byte after the last non-whitespace code point. Using the start of that code point + // (as before) cut multi-byte characters at the end of the string down to their first byte. + size_t end = begin; for (size_t i = begin; i < string->count;) { utf8proc_int32_t codepoint; auto state = utf8proc_iterate(reinterpret_cast(string->characters.get()) + i, - string->count, &codepoint); + string->count - i, &codepoint); if (state < 0) break; if (utf8proc_get_property(codepoint)->bidi_class != UTF8PROC_BIDI_CLASS_WS) { - end = i; + end = i + state; } i += state; } auto newString = String::init(); - newString->count = end - begin + 1; + newString->count = end - begin; newString->characters = runtime::allocate(string->count); std::memcpy(newString->characters.get(), string->characters.get() + begin, newString->count); return newString; } +#ifdef __wasi__ +extern "C" void sStringGraphemes(String *string, void *cbFn, void *cbCapture) { + runtime::Callable cb; + void *parts[2] = {cbFn, cbCapture}; + std::memcpy(&cb, parts, sizeof(parts)); +#else extern "C" void sStringGraphemes(String *string, runtime::Callable cb) { +#endif auto bytes = reinterpret_cast(string->characters.get()); utf8proc_int32_t state = 0; utf8proc_int32_t prev; diff --git a/s/System.cpp b/s/System.cpp index 75a92ca..35734b5 100644 --- a/s/System.cpp +++ b/s/System.cpp @@ -36,5 +36,7 @@ extern "C" runtime::SimpleOptional sSystemArg(runtime::ClassInfo*, r } extern "C" void sSystemSystem(runtime::ClassInfo*, s::String *string) { +#ifndef __wasi__ std::system(string->stdString().c_str()); +#endif }