diff --git a/libraries/native/CMakeLists.txt b/libraries/native/CMakeLists.txt index a629cde72b..f8fdee0884 100644 --- a/libraries/native/CMakeLists.txt +++ b/libraries/native/CMakeLists.txt @@ -4,10 +4,8 @@ project(native LANGUAGES CXX ASM) if (NOT __APPLE) set(CRT_ASM elf_crt.s) - set(UTILS_ASM utils.s) else() set(CRT_ASM macho_crt.s) - set(UTILS_ASM mac_utils.s) endif() if (NOT ${PARENT_PROJECT_NAME} MATCHES "cdt_tools") @@ -339,12 +337,9 @@ add_library ( sf STATIC ${softfloat_sources} ) target_include_directories( sf PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR}) if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") - # TODO: we need crt.cpp and assembly only for _prints_l and _prints - # we unlikely need those implementations for antler-run so - # we may need to add some code (macroses, etc) to crt.cpp and/or intrinsics.cpp so - # we can revert assembly changes and build only *.cpp without ${UTILS_ASM} - add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${UTILS_ASM}) + add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt_lib.cpp) + target_compile_definitions(${PROJECT_NAME} PUBLIC NATIVELIB_ENABLE_EXCEPTIONS) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/contracts ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi @@ -353,7 +348,7 @@ if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools") # suppress executable stack warning. this is due to absence of .note.GNU-stack target_link_libraries(${PROJECT_NAME} PUBLIC "-Wl,-z,noexecstack") else() - add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ${UTILS_ASM}) + add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM}) endif() target_include_directories( native PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/eosiolib/capi ${CMAKE_SOURCE_DIR}/eosiolib/contracts ${CMAKE_SOURCE_DIR}/eosiolib/core) diff --git a/libraries/native/crt_lib.cpp b/libraries/native/crt_lib.cpp new file mode 100644 index 0000000000..75fc674567 --- /dev/null +++ b/libraries/native/crt_lib.cpp @@ -0,0 +1,32 @@ +#include "native/eosio/crt.hpp" + +#include + +eosio::cdt::output_stream std_out; +eosio::cdt::output_stream std_err; + +bool ___disable_output = false; +bool ___has_failed = false; +bool ___earlier_unit_test_has_failed = false; + +void _prints_l(const char* cstr, uint32_t len, uint8_t which) { + for (int i=0; i < len; i++) { + if (which == eosio::cdt::output_stream_kind::std_out) + std_out.push(cstr[i]); + else if (which == eosio::cdt::output_stream_kind::std_err) + std_err.push(cstr[i]); + if (!___disable_output) { + std::putc(cstr[i], which == eosio::cdt::output_stream_kind::std_out ? stdout : stderr); + } + } +} +void _prints(const char* cstr, uint8_t which) { + for (int i=0; cstr[i] != '\0'; i++) { + if (which == eosio::cdt::output_stream_kind::std_out) + std_out.push(cstr[i]); + else if (which == eosio::cdt::output_stream_kind::std_err) + std_err.push(cstr[i]); + if (!___disable_output) + std::putc(cstr[i], which == eosio::cdt::output_stream_kind::std_out ? stdout : stderr); + } +} \ No newline at end of file diff --git a/libraries/native/elf_crt.s b/libraries/native/elf_crt.s index 5a452c43a6..f5f5f41810 100644 --- a/libraries/native/elf_crt.s +++ b/libraries/native/elf_crt.s @@ -1,5 +1,13 @@ .global _start +.global ___putc +.global _mmap +.global setjmp +.global longjmp .type _start,@function +.type ___putc,@function +.type _mmap,@function +.type setjmp,@function +.type longjmp,@function _start: mov %rsp, %rbp @@ -8,4 +16,60 @@ _start: call _wrap_main mov %rax, %rdi mov $60, %rax - syscall \ No newline at end of file + syscall + +___putc: + dec %rsp + mov %rbx, %r8 + mov %rdi, %rax + mov %al, 0(%rsp) + mov $1, %edi + mov %rsp, %rsi + mov $1, %edx + mov $1, %eax + syscall + inc %rsp + mov %r8, %rbx + ret + +_mmap: + mov $9, %eax + mov $0, %rdi + mov $0x6400000, %rsi # 100Mb + mov $3, %rdx + mov $0x22, %r10 + mov $-1, %r8 + mov $0, %r9 + syscall + ret + +setjmp: + mov %rbx, 0(%rdi) + mov %rbp, 8(%rdi) + mov %r12, 16(%rdi) + mov %r13, 24(%rdi) + mov %r14, 32(%rdi) + mov %r15, 40(%rdi) + lea 8(%rsp), %rdx + mov %rdx, 48(%rdi) + mov (%rsp), %rdx + mov %rdx, 56(%rdi) + xor %rax, %rax + ret + +longjmp: + mov %rsi, %rax + test %rax, %rax + jnz 1f + inc %rax +1: + mov 0(%rdi), %rbx + mov 8(%rdi), %rbp + mov 16(%rdi), %r12 + mov 24(%rdi), %r13 + mov 32(%rdi), %r14 + mov 40(%rdi), %r15 + mov 48(%rdi), %rdx + mov %rdx, %rsp + mov 56(%rdi), %rdx + jmp *%rdx diff --git a/libraries/native/mac_utils.s b/libraries/native/mac_utils.s deleted file mode 100644 index 4a7eb755ed..0000000000 --- a/libraries/native/mac_utils.s +++ /dev/null @@ -1,60 +0,0 @@ -.global ____putc -.global __mmap -.global _setjmp -.global _longjmp - -____putc: - dec %rsp - mov %rbx, %r8 - mov %rdi, %rax - mov %al, 0(%rsp) - mov $1, %edi # using stdout - mov %rsp, %rsi # point to the buffer - mov $1, %edx # buffer is only 1 char - mov $0x2000004, %eax # write syscall 0x4 - syscall - inc %rsp - mov %r8, %rbx - ret - -__mmap: - mov $0x20000C5, %eax # mmap syscall 0xC5 or 197 - mov $0, %rdi # don't map - mov $0x640000000, %rsi # size 100Mb - mov $3, %rdx - mov $0x1002, %r10 - mov $-1, %r8 - mov $0, %r9 - syscall - ret - -_setjmp: - mov %rbx, 0(%rdi) - mov %rbp, 8(%rdi) - mov %r12, 16(%rdi) - mov %r13, 24(%rdi) - mov %r14, 32(%rdi) - mov %r15, 40(%rdi) - lea 8(%rsp), %rdx - mov %rdx, 48(%rdi) - mov (%rsp), %rdx - mov %rdx, 56(%rdi) - xor %rax, %rax - ret - -_longjmp: - mov %rsi, %rax - test %rax, %rax - jnz 1f - inc %rax -1: - mov 0(%rdi), %rbx - mov 8(%rdi), %rbp - mov 16(%rdi), %r12 - mov 24(%rdi), %r13 - mov 32(%rdi), %r14 - mov 40(%rdi), %r15 - mov 48(%rdi), %rdx - mov %rdx, %rsp - mov 56(%rdi), %rdx - jmp *%rdx diff --git a/libraries/native/macho_crt.s b/libraries/native/macho_crt.s index b6cf516a9d..64fc6e405a 100644 --- a/libraries/native/macho_crt.s +++ b/libraries/native/macho_crt.s @@ -1,4 +1,8 @@ .global start +.global ____putc +.global __mmap +.global _setjmp +.global _longjmp start: mov %rsp, %rbp @@ -7,4 +11,60 @@ start: call __wrap_main mov %rax, %rdi mov $0x2000001, %rax - syscall \ No newline at end of file + syscall + +____putc: + dec %rsp + mov %rbx, %r8 + mov %rdi, %rax + mov %al, 0(%rsp) + mov $1, %edi # using stdout + mov %rsp, %rsi # point to the buffer + mov $1, %edx # buffer is only 1 char + mov $0x2000004, %eax # write syscall 0x4 + syscall + inc %rsp + mov %r8, %rbx + ret + +__mmap: + mov $0x20000C5, %eax # mmap syscall 0xC5 or 197 + mov $0, %rdi # don't map + mov $0x640000000, %rsi # size 100Mb + mov $3, %rdx + mov $0x1002, %r10 + mov $-1, %r8 + mov $0, %r9 + syscall + ret + +_setjmp: + mov %rbx, 0(%rdi) + mov %rbp, 8(%rdi) + mov %r12, 16(%rdi) + mov %r13, 24(%rdi) + mov %r14, 32(%rdi) + mov %r15, 40(%rdi) + lea 8(%rsp), %rdx + mov %rdx, 48(%rdi) + mov (%rsp), %rdx + mov %rdx, 56(%rdi) + xor %rax, %rax + ret + +_longjmp: + mov %rsi, %rax + test %rax, %rax + jnz 1f + inc %rax +1: + mov 0(%rdi), %rbx + mov 8(%rdi), %rbp + mov 16(%rdi), %r12 + mov 24(%rdi), %r13 + mov 32(%rdi), %r14 + mov 40(%rdi), %r15 + mov 48(%rdi), %rdx + mov %rdx, %rsp + mov 56(%rdi), %rdx + jmp *%rdx diff --git a/libraries/native/native/eosio/crt.hpp b/libraries/native/native/eosio/crt.hpp index 5d81a60015..d47e441b63 100644 --- a/libraries/native/native/eosio/crt.hpp +++ b/libraries/native/native/eosio/crt.hpp @@ -3,7 +3,16 @@ #include + +#ifdef NATIVELIB_ENABLE_EXCEPTIONS +#include +namespace eosio { namespace cdt { + using assert_exception = std::runtime_error; +}} //ns eosio::cdt +#endif + namespace eosio { namespace cdt { + enum output_stream_kind { std_out, std_err, @@ -27,6 +36,9 @@ extern eosio::cdt::output_stream std_out; extern eosio::cdt::output_stream std_err; extern "C" jmp_buf* ___env_ptr; extern "C" char* ___heap_ptr; +extern "C" bool ___disable_output; +extern "C" bool ___has_failed; +extern "C" bool ___earlier_unit_test_has_failed; extern "C" { void __set_env_test(); diff --git a/libraries/native/utils.s b/libraries/native/utils.s deleted file mode 100644 index a3ec67818c..0000000000 --- a/libraries/native/utils.s +++ /dev/null @@ -1,64 +0,0 @@ -.global ___putc -.global _mmap -.global setjmp -.global longjmp -.type ___putc,@function -.type _mmap,@function -.type setjmp,@function -.type longjmp,@function - -___putc: - dec %rsp - mov %rbx, %r8 - mov %rdi, %rax - mov %al, 0(%rsp) - mov $1, %edi - mov %rsp, %rsi - mov $1, %edx - mov $1, %eax - syscall - inc %rsp - mov %r8, %rbx - ret - -_mmap: - mov $9, %eax - mov $0, %rdi - mov $0x6400000, %rsi # 100Mb - mov $3, %rdx - mov $0x22, %r10 - mov $-1, %r8 - mov $0, %r9 - syscall - ret - -setjmp: - mov %rbx, 0(%rdi) - mov %rbp, 8(%rdi) - mov %r12, 16(%rdi) - mov %r13, 24(%rdi) - mov %r14, 32(%rdi) - mov %r15, 40(%rdi) - lea 8(%rsp), %rdx - mov %rdx, 48(%rdi) - mov (%rsp), %rdx - mov %rdx, 56(%rdi) - xor %rax, %rax - ret - -longjmp: - mov %rsi, %rax - test %rax, %rax - jnz 1f - inc %rax -1: - mov 0(%rdi), %rbx - mov 8(%rdi), %rbp - mov 16(%rdi), %r12 - mov 24(%rdi), %r13 - mov 32(%rdi), %r14 - mov 40(%rdi), %r15 - mov 48(%rdi), %rdx - mov %rdx, %rsp - mov 56(%rdi), %rdx - jmp *%rdx