Changeset 50b7e8c
- Timestamp:
- Apr 12, 2017, 10:15:20 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 19518e8
- Parents:
- 549950c (diff), 0d10090 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- doc/generic_types/evaluation
- Files:
-
- 6 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/generic_types/evaluation/Makefile ¶
r549950c r50b7e8c 1 1 CFA = my-cfa 2 2 DEPFLAGS = -MMD -MP 3 CFLAGS = -O2 -flto 3 CFLAGS = -O2 4 ifdef N 5 CFLAGS += -DN=$(N) 6 endif 4 7 CXXFLAGS = $(CFLAGS) --std=c++14 5 8 … … 25 28 $(COMPILE.cfa) $(OUTPUT_OPTION) -c $< 26 29 27 COBJS = c-stack.o c-pair.o 30 COBJS = c-stack.o c-pair.o c-print.o 28 31 CPPOBJS = 29 32 CPPVOBJS = cpp-vstack.o 30 CFAOBJS = cfa-stack.o cfa-pair.o 33 CFAOBJS = cfa-stack.o cfa-pair.o cfa-print.o 31 34 32 35 c-bench: c-bench.c c-bench.d $(COBJS) … … 65 68 @echo '## Cforall ##' 66 69 @/usr/bin/time -f 'max_memory:\t %M kilobytes' ./cfa-bench 67 @printf 'source_size:\t%8d lines\n' `cat cfa-bench.c bench.h cfa-stack.h cfa-stack.c | wc -l`70 @printf 'source_size:\t%8d lines\n' `cat cfa-bench.c bench.h cfa-stack.h cfa-stack.c cfa-print.h cfa-print.c | wc -l` 68 71 @printf 'binary_size:\t%8d bytes\n' `stat -c %s cfa-bench` 69 72 … … 72 75 @echo '## C++ ##' 73 76 @/usr/bin/time -f 'max_memory:\t %M kilobytes' ./cpp-bench 74 @printf 'source_size:\t%8d lines\n' `cat cpp-bench.cpp bench.hpp cpp-stack.hpp | wc -l`77 @printf 'source_size:\t%8d lines\n' `cat cpp-bench.cpp bench.hpp cpp-stack.hpp cpp-print.hpp | wc -l` 75 78 @printf 'binary_size:\t%8d bytes\n' `stat -c %s cpp-bench` 76 79 -
TabularUnified doc/generic_types/evaluation/bench.h ¶
r549950c r50b7e8c 4 4 #include <time.h> 5 5 6 #define N 50000000 6 #ifndef N 7 #define N 40000000 8 #endif 7 9 8 10 long ms_between(clock_t start, clock_t end) { -
TabularUnified doc/generic_types/evaluation/bench.hpp ¶
r549950c r50b7e8c 5 5 #include <time.h> 6 6 7 static const int N = 50000000; 7 #ifndef N 8 static const int N = 40000000; 9 #endif 8 10 9 11 long ms_between(clock_t start, clock_t end) { -
TabularUnified doc/generic_types/evaluation/c-bench.c ¶
r549950c r50b7e8c 1 #include <stdio.h> 1 2 #include <stdlib.h> 2 3 #include "bench.h" 3 4 #include "c-pair.h" 4 5 #include "c-stack.h" 6 #include "c-print.h" 5 7 6 8 _Bool* new_bool( _Bool b ) { … … 89 91 ) 90 92 free_pair_bool_char( max2 ); 93 94 FILE* out = fopen("c-out.txt", "w"); 95 REPEAT_TIMED( "print_int", 96 print( out, "dsds", rand(), ":", rand(), "\n" ); 97 ) 98 99 REPEAT_TIMED( "print_pair", 100 struct pair p1 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); 101 struct pair p2 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); 102 print( out, "pbcspbcs", p1, ":", p2, "\n" ); 103 free(p1.first); free(p1.second); 104 free(p2.first); free(p2.second); 105 ) 106 fclose(out); 91 107 } -
TabularUnified doc/generic_types/evaluation/cfa-bench.c ¶
r549950c r50b7e8c 1 1 #include <stdlib> 2 2 #include <stdlib.h> 3 #include <stdio.h> 3 4 #include "pair" 4 5 #include "bench.h" 5 6 #include "cfa-stack.h" 7 #include "cfa-print.h" 6 8 7 9 int main(int argc, char** argv) { … … 45 47 max2 = max( max2, pop( &t2 ) ); 46 48 ) 49 50 FILE* out = fopen("cfa-out.txt", "w"); 51 REPEAT_TIMED( "print_int", 52 print( out, rand(), ":", rand(), "\n" ); 53 ) 54 55 REPEAT_TIMED( "print_pair", 56 print( out, (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, ":", 57 (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, "\n" ); 58 ) 59 fclose(out); 47 60 } -
TabularUnified doc/generic_types/evaluation/cpp-bench.cpp ¶
r549950c r50b7e8c 1 1 #include <algorithm> 2 #include <fstream> 2 3 #include <stdlib.h> 3 4 #include <utility> 4 5 #include "bench.hpp" 5 6 #include "cpp-stack.hpp" 7 #include "cpp-print.hpp" 6 8 7 9 int main(int argc, char** argv) { … … 45 47 max2 = std::max( max2, t2.pop() ); 46 48 ) 49 50 std::ofstream out{"cpp-out.txt"}; 51 REPEAT_TIMED( "print_int", 52 print( out, rand(), ":", rand(), "\n" ); 53 ) 54 55 REPEAT_TIMED( "print_pair", 56 print( out, std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, ":", 57 std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, "\n" ); 58 ) 47 59 } -
TabularUnified doc/generic_types/evaluation/cpp-stack.hpp ¶
r549950c r50b7e8c 33 33 delete crnt; 34 34 } 35 head = nullptr; 35 36 } 36 37 -
TabularUnified doc/generic_types/evaluation/cpp-vbench.cpp ¶
r549950c r50b7e8c 1 1 #include <algorithm> 2 #include <fstream> 2 3 #include <stdlib.h> 3 4 #include "bench.hpp" 4 5 #include "cpp-vstack.hpp" 6 #include "cpp-vprint.hpp" 5 7 #include "object.hpp" 6 8 … … 45 47 std::make_unique<character>('\0') ); 46 48 REPEAT_TIMED( "pop_bool_char", 47 std::unique_ptr<object> x = t2.pop(); 48 if ( x->as<pair>() > *max2 ) { max2.reset( static_cast<pair*>(x.release()) ); } 49 std::unique_ptr<pair> x = as_ptr<pair>( t2.pop() ); 50 if ( *x > *max2 ) { max2 = std::move(x); } 51 ) 52 53 std::ofstream out{"cpp-vout.txt"}; 54 REPEAT_TIMED( "print_int", 55 print( out, integer{rand()}, c_string{":"}, integer{rand()}, c_string{"\n"} ); 56 ) 57 58 REPEAT_TIMED( "print_pair", 59 print( out, pair{ std::make_unique<boolean>( rand() & 0x1 ), 60 std::make_unique<character>( rand() & 0x7F ) }, c_string{":"}, 61 pair{ std::make_unique<boolean>( rand() & 0x1 ), 62 std::make_unique<character>( rand() & 0x7F ) }, c_string{"\n"} ); 49 63 ) 50 64 } -
TabularUnified doc/generic_types/evaluation/object.hpp ¶
r549950c r50b7e8c 1 1 #pragma once 2 2 3 #include <cstddef> 3 4 #include <exception> 5 #include <iomanip> 4 6 #include <memory> 7 #include <ostream> 5 8 #include <string> 6 9 #include <typeinfo> … … 24 27 class object { 25 28 public: 26 std::type_index get_class() const { return { t ypeid(*this) }; }29 std::type_index get_class() const { return { this ? typeid(*this) : typeid(std::nullptr_t) }; } 27 30 28 31 template<typename T> 29 32 T& as() { 30 std::type_index from = get_class(), to = class_of<T>();31 if ( from != to ) throw bad_cast{ from, to};32 return reinterpret_cast<T&>(*this);33 T* p = dynamic_cast<T*>(this); 34 if ( !p ) throw bad_cast{ get_class(), class_of<T>() }; 35 return *p; 33 36 } 34 37 35 38 template<typename T> 36 39 const T& as() const { 37 std::type_index from = get_class(), to = class_of<T>();38 if ( from != to ) throw bad_cast{ from, to};39 return reinterpret_cast<const T&>(*this);40 const T* p = dynamic_cast<const T*>(this); 41 if ( !p ) throw bad_cast{ get_class(), class_of<T>() }; 42 return *p; 40 43 } 41 44 … … 49 52 }; 50 53 51 template<typename T> 52 T* as_subclass_of( object* o ) { 53 T* r = dynamic_cast<T*>( o ); 54 if ( r == nullptr ) throw bad_cast{ o->get_class(), class_of<T>() }; 55 return r; 54 template<typename To, typename From> 55 std::unique_ptr<To> as_ptr( std::unique_ptr<From>&& p ) { 56 return std::unique_ptr<To>{ &p.release()->template as<To>() }; 56 57 } 57 58 58 template<typename T> 59 const T* as_subclass_of( const object* o ) { 60 const T* r = dynamic_cast<const T*>( o ); 61 if ( r == nullptr ) throw bad_cast{ o->get_class(), class_of<T>() }; 62 return r; 63 } 64 65 class ordered : public object { 59 class ordered : public virtual object { 66 60 public: 67 61 virtual int cmp(const ordered&) const = 0; … … 80 74 }; 81 75 82 class boolean : public ordered { 83 private: 76 class printable : public virtual object { 77 public: 78 virtual void print(std::ostream&) const = 0; 79 }; 80 81 class boolean : public ordered, public printable { 84 82 bool x; 85 83 … … 98 96 } 99 97 100 object& operator= (const object& that) override { 101 std::type_index from = that.get_class(), to = get_class(); 102 if ( from != to ) throw bad_cast{ from, to }; 103 return *this = static_cast<const boolean&>(that); 104 } 98 object& operator= (const object& that) override { return *this = that.as<boolean>(); } 105 99 106 100 ~boolean() override = default; … … 108 102 int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; } 109 103 110 // bool operator< (const boolean& that) const { return x < that.x; } 111 112 // bool operator== (const boolean& that) const { return x == that.x; } 113 114 int cmp(const ordered& that) const override { 115 std::type_index from = that.get_class(), to = get_class(); 116 if ( from != to ) throw bad_cast{ from, to }; 117 return cmp( static_cast<const boolean&>(that) ); 118 } 119 }; 120 121 class character : public ordered { 122 private: 104 int cmp(const ordered& that) const override { return cmp( that.as<boolean>() ); } 105 106 void print(std::ostream& out) const override { out << (x ? "true" : "false"); } 107 }; 108 109 class character : public ordered, public printable { 123 110 char x; 124 111 … … 137 124 } 138 125 139 object& operator= (const object& that) override { 140 std::type_index from = that.get_class(), to = get_class(); 141 if ( from != to ) throw bad_cast{ from, to }; 142 return *this = static_cast<const character&>(that); 143 } 126 object& operator= (const object& that) override { return *this = that.as<character>(); } 144 127 145 128 ~character() override = default; … … 147 130 int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; } 148 131 149 // bool operator< (const character& that) const { return x < that.x; } 150 151 // bool operator== (const character& that) const { return x == that.x; } 152 153 int cmp(const ordered& that) const override { 154 std::type_index from = that.get_class(), to = get_class(); 155 if ( from != to ) throw bad_cast{ from, to }; 156 return cmp( static_cast<const character&>(that) ); 157 } 158 }; 159 160 class integer : public ordered { 161 private: 132 int cmp(const ordered& that) const override { return cmp( that.as<character>() ); } 133 134 void print(std::ostream& out) const override { 135 if ( 0x20 <= x && x <= 0x7E ) { out << "'" << x << "'"; } 136 else { out << "'\\" << std::hex << (unsigned int)x << std::setbase(0) << "'"; } 137 } 138 }; 139 140 class integer : public ordered, public printable { 162 141 int x; 163 142 … … 176 155 } 177 156 178 object& operator= (const object& that) override { 179 std::type_index from = that.get_class(), to = get_class(); 180 if ( from != to ) throw bad_cast{ from, to }; 181 return *this = static_cast<const integer&>(that); 182 } 157 object& operator= (const object& that) override { return *this = that.as<integer>(); } 183 158 184 159 ~integer() override = default; … … 186 161 int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; } 187 162 188 // bool operator< (const integer& that) const { return x < that.x; } 189 190 // bool operator== (const integer& that) const { return x == that.x; } 191 192 int cmp(const ordered& that) const override { 193 std::type_index from = that.get_class(), to = get_class(); 194 if ( from != to ) throw bad_cast{ from, to }; 195 return cmp( static_cast<const integer&>(that) ); 196 } 197 }; 198 199 class pair : public ordered { 200 private: 163 int cmp(const ordered& that) const override { return cmp( that.as<integer>() ); } 164 165 void print(std::ostream& out) const override { out << x; } 166 }; 167 168 class c_string : public printable { 169 static constexpr const char* empty = ""; 170 const char* s; 171 public: 172 c_string() : s(empty) {} 173 174 c_string(const char* s) : s(s) {} 175 176 std::unique_ptr<object> new_inst() const override { return std::make_unique<c_string>(); } 177 178 std::unique_ptr<object> new_copy() const override { return std::make_unique<c_string>(s); } 179 180 c_string& operator= (const c_string& that) { 181 s = that.s; 182 return *this; 183 } 184 185 object& operator= (const object& that) override { return *this = that.as<c_string>(); } 186 187 ~c_string() override = default; 188 189 void print(std::ostream& out) const override { out << s; } 190 }; 191 192 class pair : public ordered, public printable { 201 193 std::unique_ptr<object> x; 202 194 std::unique_ptr<object> y; … … 220 212 } 221 213 222 object& operator= (const object& that) override { 223 std::type_index from = that.get_class(), to = get_class(); 224 if ( from != to ) throw bad_cast{ from, to }; 225 return *this = static_cast<const pair&>(that); 226 } 214 object& operator= (const object& that) override { return *this = that.as<pair>(); } 227 215 228 216 ~pair() override = default; 229 217 230 218 int cmp(const pair& that) const { 231 const ordered* a = as_subclass_of<ordered>( x.get() ); 232 const ordered* b = as_subclass_of<ordered>( that.x.get() ); 233 int c = a->cmp( *b ); 219 int c = x->as<ordered>().cmp( that.x->as<ordered>() ); 234 220 if ( c != 0 ) return c; 235 a = as_subclass_of<ordered>( y.get() ); 236 b = as_subclass_of<ordered>( that.y.get() ); 237 return a->cmp( *b ); 238 } 239 240 // bool operator< (const pair& that) const { return cmp(that) < 0; } 241 242 // bool operator== ( const pair& that) const { return cmp(that) == 0; } 243 244 int cmp(const ordered& that) const override { 245 std::type_index from = that.get_class(), to = get_class(); 246 if ( from != to ) throw bad_cast{ from, to }; 247 return cmp( static_cast<const pair&>(that) ); 248 } 249 }; 221 return y->as<ordered>().cmp( that.y->as<ordered>() ); 222 } 223 224 int cmp(const ordered& that) const override { return cmp( that.as<pair>() ); } 225 226 void print(std::ostream& out) const override { 227 out << "["; 228 x->as<printable>().print(out); 229 out << ", "; 230 y->as<printable>().print(out); 231 out << "]"; 232 } 233 };
Note: See TracChangeset
for help on using the changeset viewer.