From 53095a15c500b5bb216727a86f21cd7fc9530e3f Mon Sep 17 00:00:00 2001 From: John Hodge Date: Mon, 15 May 2017 12:13:35 +0800 Subject: Codegen C - Fix some edge cases with floats, add newer intrinsics --- src/trans/codegen_c.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/trans') diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index 2d7a521d..5ea03eca 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -1062,7 +1062,15 @@ namespace { } ), (Float, - m_of << e; + if( ::std::isnan(e) ) { + m_of << "NAN"; + } + else if( ::std::isinf(e) ) { + m_of << "INFINITY"; + } + else { + m_of << e; + } ), (BorrowOf, TU_MATCHA( (e.m_data), (pe), @@ -2273,6 +2281,12 @@ namespace { else if( name == "unchecked_rem" ) { emit_lvalue(e.ret_val); m_of << " = "; emit_param(e.args.at(0)); m_of << " % "; emit_param(e.args.at(1)); } + else if( name == "unchecked_shl" ) { + emit_lvalue(e.ret_val); m_of << " = "; emit_param(e.args.at(0)); m_of << " << "; emit_param(e.args.at(1)); + } + else if( name == "unchecked_shr" ) { + emit_lvalue(e.ret_val); m_of << " = "; emit_param(e.args.at(0)); m_of << " >> "; emit_param(e.args.at(1)); + } // Bit Twiddling // - CounT Leading Zeroes // - CounT Trailing Zeroes @@ -2463,6 +2477,9 @@ namespace { auto ordering = H::get_atomic_ordering(mir_res, name, 7+6); m_of << "atomic_thread_fence(" << ordering << ")"; } + else if( name == "atomic_singlethreadfence" || name.compare(0, 7+18, "atomic_singlethreadfence_") == 0 ) { + // TODO: Does this matter? + } else { MIR_BUG(mir_res, "Unknown intrinsic '" << name << "'"); } @@ -2718,7 +2735,8 @@ namespace { emit_literal(ty, lit, {}); ), (Float, - emit_dst(); m_of << " = " << e; + emit_dst(); m_of << " = "; + emit_literal(ty, lit, {}); ), (BorrowOf, if( ty.m_data.is_Function() ) -- cgit v1.2.3 From 74759c0172a4aac97a3f628e3adc92c3cdb731f3 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 26 May 2017 12:04:39 +0800 Subject: Codegen C - Handle signed bswap --- Makefile | 15 +++++---------- src/trans/codegen_c.cpp | 8 +++++++- 2 files changed, 12 insertions(+), 11 deletions(-) (limited to 'src/trans') diff --git a/Makefile b/Makefile index a7b3010d..498a6b8e 100644 --- a/Makefile +++ b/Makefile @@ -372,8 +372,6 @@ DISABLED_TESTS += run-pass/out-of-stack # - Requires jemalloc DISABLED_TESTS += run-pass/allocator-default DISABLED_TESTS += run-pass/allocator-override -# - Bug in inferrence order. -DISABLED_TESTS += run-pass/associated-types-conditional-dispatch # - Lazy. DISABLED_TESTS += run-pass/associated-types-projection-in-where-clause # Not normalizing bounds DISABLED_TESTS += run-pass/cast # Disallows cast from char to i32 @@ -431,27 +429,24 @@ DISABLED_TESTS += run-pass/regions-infer-borrow-scope-addr-of # Didn't unify lit DISABLED_TESTS += run-pass/swap-2 # ^ DISABLED_TESTS += run-pass/slice_binary_search # Didn't detect infer possiblity (&str, &String) # - Lazy (Typecheck - Array unsize) +DISABLED_TESTS += run-pass/associated-types-conditional-dispatch # Ran through coercion point DISABLED_TESTS += run-pass/byte-literals # Over-eager inferrence DISABLED_TESTS += run-pass/never_coercions # Over-eager inferrence (ran through coercion) DISABLED_TESTS += run-pass/cast-rfc0401-vtable-kinds # Spare rules (struct Unsize) DISABLED_TESTS += run-pass/dst-struct-sole # ^ DISABLED_TESTS += run-pass/dst-struct # ^ DISABLED_TESTS += run-pass/issue-23261 # ^ -DISABLED_TESTS += run-pass/cast-rfc0401 # Skipped coerce unsized -DISABLED_TESTS += run-pass/fat-ptr-cast # Skipped coerce unsized -DISABLED_TESTS += run-pass/issue-21562 # Skipped coerce unsized - ERROR - Borrow->Pointer and Unsize in one +DISABLED_TESTS += run-pass/cast-rfc0401 # Skipped coerce unsized - Cast to raw pointer causing unsize +DISABLED_TESTS += run-pass/fat-ptr-cast # Skipped coerce unsized - ERROR - coerce Borrow->Pointer and Unsize in one +DISABLED_TESTS += run-pass/issue-21562 # ^ DISABLED_TESTS += run-pass/mir_raw_fat_ptr # ^ DISABLED_TESTS += run-pass/raw-fat-ptr # ^ -## - Lazy (Typecheck + Trait unsize) -#DISABLED_TESTS += run-pass/issue-27105 -#DISABLED_TESTS += run-pass/dst-coerce-rc +## - Lazy (Typecheck - Trait unsize) DISABLED_TESTS += run-pass/dst-coercions # Skipped CoerceUnsize DISABLED_TESTS += run-pass/dst-raw # Skipped CoerceUnsize DISABLED_TESTS += run-pass/issue-11677 # Skipped -#DISABLED_TESTS += run-pass/dst-trait # - Lazy (MIR) DISABLED_TESTS += run-pass/if-ret # If condition wasn't a bool -DISABLED_TESTS += run-pass/intrinsics-integer # todo - bswap DISABLED_TESTS += run-pass/issue-11940 # todo: Match literal Borrow DISABLED_TESTS += run-pass/mir_build_match_comparisons # - ^ DISABLED_TESTS += run-pass/issue-18352 # - ^ diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index 5ea03eca..7a46306d 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -195,7 +195,8 @@ namespace { // TODO: use a formatter specific to shell escaping cmd_ss << "\"" << FmtEscaped(arg) << "\" "; } - DEBUG("- " << cmd_ss.str()); + //DEBUG("- " << cmd_ss.str()); + ::std::cout << "Running comamnd - " << cmd_ss.str() << ::std::endl; if( system(cmd_ss.str().c_str()) ) { abort(); @@ -2194,18 +2195,23 @@ namespace { switch( ty.m_data.as_Primitive() ) { case ::HIR::CoreType::U8: + case ::HIR::CoreType::I8: emit_lvalue(e.ret_val); m_of << " = "; emit_param(e.args.at(0)); break; case ::HIR::CoreType::U16: + case ::HIR::CoreType::I16: emit_lvalue(e.ret_val); m_of << " = __builtin_bswap16("; emit_param(e.args.at(0)); m_of << ")"; break; case ::HIR::CoreType::U32: + case ::HIR::CoreType::I32: emit_lvalue(e.ret_val); m_of << " = __builtin_bswap32("; emit_param(e.args.at(0)); m_of << ")"; break; case ::HIR::CoreType::U64: + case ::HIR::CoreType::I64: emit_lvalue(e.ret_val); m_of << " = __builtin_bswap64("; emit_param(e.args.at(0)); m_of << ")"; break; case ::HIR::CoreType::U128: + case ::HIR::CoreType::I128: emit_lvalue(e.ret_val); m_of << " = __builtin_bswap128("; emit_param(e.args.at(0)); m_of << ")"; break; default: -- cgit v1.2.3 From f4614d4de8b72dfb493be7530800624fc70fb696 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 2 Jun 2017 12:28:13 +0800 Subject: Codegen C - Return value from main --- src/trans/codegen_c.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/trans') diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index 7a46306d..07736df3 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -119,13 +119,13 @@ namespace { auto c_start_path = m_resolve.m_crate.get_lang_item_path_opt("mrustc-start"); if( c_start_path == ::HIR::SimplePath() ) { - m_of << "\t" << Trans_Mangle( ::HIR::GenericPath(m_resolve.m_crate.get_lang_item_path(Span(), "start")) ) << "(" + m_of << "\treturn " << Trans_Mangle( ::HIR::GenericPath(m_resolve.m_crate.get_lang_item_path(Span(), "start")) ) << "(" << "(uint8_t*)" << Trans_Mangle( ::HIR::GenericPath(m_resolve.m_crate.get_lang_item_path(Span(), "mrustc-main")) ) << ", argc, (uint8_t**)argv" << ");\n"; } else { - m_of << "\t" << Trans_Mangle(::HIR::GenericPath(c_start_path)) << "(argc, argv);\n"; + m_of << "\treturn " << Trans_Mangle(::HIR::GenericPath(c_start_path)) << "(argc, argv);\n"; } m_of << "}\n"; } -- cgit v1.2.3 From 64ad08b84bef88228e8f6cc07803a7f644f7b22b Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 4 Jun 2017 15:24:10 +0800 Subject: Trans - Emit monomorphised/inlined functions as `static` --- src/trans/codegen.cpp | 20 +++++++++++++------- src/trans/codegen.hpp | 4 ++-- src/trans/codegen_c.cpp | 13 +++++++++++-- src/trans/mangling.cpp | 2 +- 4 files changed, 27 insertions(+), 12 deletions(-) (limited to 'src/trans') diff --git a/src/trans/codegen.cpp b/src/trans/codegen.cpp index 7989981b..a571b5e4 100644 --- a/src/trans/codegen.cpp +++ b/src/trans/codegen.cpp @@ -87,16 +87,18 @@ void Trans_Codegen(const ::std::string& outfile, const TransOptions& opt, const { DEBUG("FUNCTION " << ent.first); assert( ent.second->ptr ); - if( ent.second->ptr->m_code.m_mir ) { - codegen->emit_function_proto(ent.first, *ent.second->ptr, ent.second->pp); + const auto& fcn = *ent.second->ptr; + bool is_extern = ! static_cast(fcn.m_code); + if( fcn.m_code.m_mir ) { + codegen->emit_function_proto(ent.first, fcn, ent.second->pp, is_extern); } else { // TODO: Why would an intrinsic be in the queue? // - If it's exported it does. - if( ent.second->ptr->m_abi == "rust-intrinsic" ) { + if( fcn.m_abi == "rust-intrinsic" ) { } else { - codegen->emit_function_ext(ent.first, *ent.second->ptr, ent.second->pp); + codegen->emit_function_ext(ent.first, fcn, ent.second->pp); } } } @@ -148,7 +150,8 @@ void Trans_Codegen(const ::std::string& outfile, const TransOptions& opt, const const auto& pp = ent.second->pp; TRACE_FUNCTION_F(path); DEBUG("FUNCTION CODE " << path); - // TODO: If this is a provided trait method, it needs to be monomorphised too. + bool is_extern = ! static_cast(fcn.m_code); + // If this is a provided trait method, it needs to be monomorphised too. bool is_method = ( fcn.m_args.size() > 0 && visit_ty_with(fcn.m_args[0].second, [&](const auto& x){return x == ::HIR::TypeRef("Self",0xFFFF);}) ); if( pp.has_types() || is_method ) { @@ -164,11 +167,14 @@ void Trans_Codegen(const ::std::string& outfile, const TransOptions& opt, const MIR_Cleanup(resolve, ip, *mir, args, ret_type); MIR_Optimise(resolve, ip, *mir, args, ret_type); MIR_Validate(resolve, ip, *mir, args, ret_type); - codegen->emit_function_code(path, fcn, ent.second->pp, mir); + // TODO: Flag that this should be a weak (or weak-er) symbol? + // - If it's from an external crate, it should be weak + codegen->emit_function_code(path, fcn, ent.second->pp, is_extern, mir); } // TODO: Detect if the function was a #[inline] function from another crate, and don't emit if that is the case? + // - Emiting is nice, but it should be emitted as a weak symbol else { - codegen->emit_function_code(path, fcn, pp, fcn.m_code.m_mir); + codegen->emit_function_code(path, fcn, pp, is_extern, fcn.m_code.m_mir); } } } diff --git a/src/trans/codegen.hpp b/src/trans/codegen.hpp index b317e632..65135d18 100644 --- a/src/trans/codegen.hpp +++ b/src/trans/codegen.hpp @@ -50,8 +50,8 @@ public: virtual void emit_static_local(const ::HIR::Path& p, const ::HIR::Static& item, const Trans_Params& params) {} virtual void emit_function_ext(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params) {} - virtual void emit_function_proto(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params) {} - virtual void emit_function_code(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, const ::MIR::FunctionPointer& code) {} + virtual void emit_function_proto(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, bool is_extern_def) {} + virtual void emit_function_code(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, bool is_extern_def, const ::MIR::FunctionPointer& code) {} }; diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index 07736df3..946c7fac 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -426,6 +426,8 @@ namespace { ::std::vector< ::std::pair<::HIR::Pattern,::HIR::TypeRef> > args; if( item.m_markings.has_drop_impl ) { + if( p.m_path.m_crate_name != m_crate.m_crate_name ) + m_of << "static "; m_of << "tUNIT " << Trans_Mangle( ::HIR::Path(struct_ty.clone(), m_resolve.m_lang_Drop, "drop") ) << "("; emit_ctype(struct_ty_ptr, FMT_CB(ss, ss << "rv";)); m_of << ");\n"; } else if( m_resolve.is_type_owned_box(struct_ty) ) @@ -1255,7 +1257,7 @@ namespace { m_mir_res = nullptr; } - void emit_function_proto(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params) override + void emit_function_proto(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, bool is_extern_def) override { ::MIR::TypeResolve top_mir_res { sp, m_resolve, FMT_CB(ss, ss << "/*proto*/ fn " << p;), ::HIR::TypeRef(), {}, *(::MIR::Function*)nullptr }; m_mir_res = &top_mir_res; @@ -1265,12 +1267,16 @@ namespace { { m_of << "#define " << Trans_Mangle(p) << " " << item.m_linkage.name << "\n"; } + if( is_extern_def ) + { + m_of << "static "; + } emit_function_header(p, item, params); m_of << ";\n"; m_mir_res = nullptr; } - void emit_function_code(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, const ::MIR::FunctionPointer& code) override + void emit_function_code(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, bool is_extern_def, const ::MIR::FunctionPointer& code) override { TRACE_FUNCTION_F(p); @@ -1285,6 +1291,9 @@ namespace { m_mir_res = &mir_res; m_of << "// " << p << "\n"; + if( is_extern_def ) { + m_of << "static "; + } emit_function_header(p, item, params); m_of << "\n"; m_of << "{\n"; diff --git a/src/trans/mangling.cpp b/src/trans/mangling.cpp index 4ea56581..0f3acfd9 100644 --- a/src/trans/mangling.cpp +++ b/src/trans/mangling.cpp @@ -148,7 +148,7 @@ namespace { ), (Tuple, return FMT_CB(ss, - ss << "$T"; + ss << "$T" << te.size(); for(const auto& t : te) ss << "_" << Trans_Mangle(t); ); -- cgit v1.2.3