Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(665)

Side by Side Diff: src/wasm/asm-wasm-builder.cc

Issue 1702293002: Add asm.js stdlib portion implementable as wasm opcodes. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: fix win Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 // Required to get M_E etc. in MSVC.
8 #if defined(_WIN32)
9 #define _USE_MATH_DEFINES
10 #endif
11 #include <math.h>
12
7 #include "src/wasm/asm-wasm-builder.h" 13 #include "src/wasm/asm-wasm-builder.h"
8 #include "src/wasm/wasm-macro-gen.h" 14 #include "src/wasm/wasm-macro-gen.h"
9 #include "src/wasm/wasm-opcodes.h" 15 #include "src/wasm/wasm-opcodes.h"
10 16
11 #include "src/ast/ast.h" 17 #include "src/ast/ast.h"
12 #include "src/ast/scopes.h" 18 #include "src/ast/scopes.h"
13 #include "src/codegen.h" 19 #include "src/codegen.h"
14 #include "src/type-cache.h" 20 #include "src/type-cache.h"
15 21
16 namespace v8 { 22 namespace v8 {
17 namespace internal { 23 namespace internal {
18 namespace wasm { 24 namespace wasm {
19 25
20 #define RECURSE(call) \ 26 #define RECURSE(call) \
21 do { \ 27 do { \
22 DCHECK(!HasStackOverflow()); \ 28 DCHECK(!HasStackOverflow()); \
23 call; \ 29 call; \
24 if (HasStackOverflow()) return; \ 30 if (HasStackOverflow()) return; \
25 } while (false) 31 } while (false)
26 32
27 33
28 class AsmWasmBuilderImpl : public AstVisitor { 34 class AsmWasmBuilderImpl : public AstVisitor {
29 public: 35 public:
30 AsmWasmBuilderImpl(Isolate* isolate, Zone* zone, FunctionLiteral* literal, 36 AsmWasmBuilderImpl(Isolate* isolate, Zone* zone, FunctionLiteral* literal,
31 Handle<Object> foreign) 37 Handle<Object> foreign, AsmTyper* typer)
32 : local_variables_(HashMap::PointersMatch, 38 : local_variables_(HashMap::PointersMatch,
33 ZoneHashMap::kDefaultHashMapCapacity, 39 ZoneHashMap::kDefaultHashMapCapacity,
34 ZoneAllocationPolicy(zone)), 40 ZoneAllocationPolicy(zone)),
35 functions_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity, 41 functions_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity,
36 ZoneAllocationPolicy(zone)), 42 ZoneAllocationPolicy(zone)),
37 global_variables_(HashMap::PointersMatch, 43 global_variables_(HashMap::PointersMatch,
38 ZoneHashMap::kDefaultHashMapCapacity, 44 ZoneHashMap::kDefaultHashMapCapacity,
39 ZoneAllocationPolicy(zone)), 45 ZoneAllocationPolicy(zone)),
40 in_function_(false), 46 in_function_(false),
41 is_set_op_(false), 47 is_set_op_(false),
42 marking_exported(false), 48 marking_exported(false),
43 builder_(new (zone) WasmModuleBuilder(zone)), 49 builder_(new (zone) WasmModuleBuilder(zone)),
44 current_function_builder_(nullptr), 50 current_function_builder_(nullptr),
45 literal_(literal), 51 literal_(literal),
46 isolate_(isolate), 52 isolate_(isolate),
47 zone_(zone), 53 zone_(zone),
48 foreign_(foreign), 54 foreign_(foreign),
55 typer_(typer),
49 cache_(TypeCache::Get()), 56 cache_(TypeCache::Get()),
50 breakable_blocks_(zone), 57 breakable_blocks_(zone),
51 block_size_(0), 58 block_size_(0),
52 init_function_index_(0), 59 init_function_index_(0),
53 next_table_index_(0), 60 next_table_index_(0),
54 function_tables_(HashMap::PointersMatch, 61 function_tables_(HashMap::PointersMatch,
55 ZoneHashMap::kDefaultHashMapCapacity, 62 ZoneHashMap::kDefaultHashMapCapacity,
56 ZoneAllocationPolicy(zone)), 63 ZoneAllocationPolicy(zone)),
57 imported_function_table_(this) { 64 imported_function_table_(this) {
58 InitializeAstVisitor(isolate); 65 InitializeAstVisitor(isolate);
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 } 371 }
365 372
366 void VisitConditional(Conditional* expr) { 373 void VisitConditional(Conditional* expr) {
367 DCHECK(in_function_); 374 DCHECK(in_function_);
368 current_function_builder_->Emit(kExprIfElse); 375 current_function_builder_->Emit(kExprIfElse);
369 RECURSE(Visit(expr->condition())); 376 RECURSE(Visit(expr->condition()));
370 RECURSE(Visit(expr->then_expression())); 377 RECURSE(Visit(expr->then_expression()));
371 RECURSE(Visit(expr->else_expression())); 378 RECURSE(Visit(expr->else_expression()));
372 } 379 }
373 380
381 bool VisitStdlibConstant(Variable* var) {
382 AsmTyper::StandardMember standard_object =
383 typer_->VariableAsStandardMember(var);
384 switch (standard_object) {
385 case AsmTyper::kInfinity: {
386 double infinity = std::numeric_limits<double>::infinity();
387 byte code[] = {WASM_F64(infinity)};
388 current_function_builder_->EmitCode(code, sizeof(code));
aseemgarg 2016/02/22 22:15:53 move the EmitCode and return to outside of cases a
bradnelson 2016/02/23 09:48:54 Done.
389 return true;
390 }
391 case AsmTyper::kNaN: {
392 double nan = std::numeric_limits<double>::signaling_NaN();
393 byte code[] = {WASM_F64(nan)};
394 current_function_builder_->EmitCode(code, sizeof(code));
395 return true;
396 }
397 case AsmTyper::kMathE: {
398 byte code[] = {WASM_F64(M_E)};
399 current_function_builder_->EmitCode(code, sizeof(code));
400 return true;
401 }
402 case AsmTyper::kMathLN10: {
403 byte code[] = {WASM_F64(M_LN10)};
404 current_function_builder_->EmitCode(code, sizeof(code));
405 return true;
406 }
407 case AsmTyper::kMathLN2: {
408 byte code[] = {WASM_F64(M_LN2)};
409 current_function_builder_->EmitCode(code, sizeof(code));
410 return true;
411 }
412 case AsmTyper::kMathLOG10E: {
413 byte code[] = {WASM_F64(M_LOG10E)};
414 current_function_builder_->EmitCode(code, sizeof(code));
415 return true;
416 }
417 case AsmTyper::kMathLOG2E: {
418 byte code[] = {WASM_F64(M_LOG2E)};
419 current_function_builder_->EmitCode(code, sizeof(code));
420 return true;
421 }
422 case AsmTyper::kMathPI: {
423 byte code[] = {WASM_F64(M_PI)};
424 current_function_builder_->EmitCode(code, sizeof(code));
425 return true;
426 }
427 case AsmTyper::kMathSQRT1_2: {
428 byte code[] = {WASM_F64(M_SQRT1_2)};
429 current_function_builder_->EmitCode(code, sizeof(code));
430 return true;
431 }
432 case AsmTyper::kMathSQRT2: {
433 byte code[] = {WASM_F64(M_SQRT2)};
434 current_function_builder_->EmitCode(code, sizeof(code));
435 return true;
436 }
437 default: { break; }
438 }
439 return false;
440 }
441
374 void VisitVariableProxy(VariableProxy* expr) { 442 void VisitVariableProxy(VariableProxy* expr) {
375 if (in_function_) { 443 if (in_function_) {
376 Variable* var = expr->var(); 444 Variable* var = expr->var();
377 if (is_set_op_) { 445 if (is_set_op_) {
378 if (var->IsContextSlot()) { 446 if (var->IsContextSlot()) {
379 current_function_builder_->Emit(kExprStoreGlobal); 447 current_function_builder_->Emit(kExprStoreGlobal);
380 } else { 448 } else {
381 current_function_builder_->Emit(kExprSetLocal); 449 current_function_builder_->Emit(kExprSetLocal);
382 } 450 }
383 is_set_op_ = false; 451 is_set_op_ = false;
384 } else { 452 } else {
453 if (VisitStdlibConstant(var)) {
454 return;
455 }
385 if (var->IsContextSlot()) { 456 if (var->IsContextSlot()) {
386 current_function_builder_->Emit(kExprLoadGlobal); 457 current_function_builder_->Emit(kExprLoadGlobal);
387 } else { 458 } else {
388 current_function_builder_->Emit(kExprGetLocal); 459 current_function_builder_->Emit(kExprGetLocal);
389 } 460 }
390 } 461 }
391 LocalType var_type = TypeOf(expr); 462 LocalType var_type = TypeOf(expr);
392 DCHECK_NE(kAstStmt, var_type); 463 DCHECK_NE(kAstStmt, var_type);
393 if (var->IsContextSlot()) { 464 if (var->IsContextSlot()) {
394 AddLeb128(LookupOrInsertGlobal(var, var_type), false); 465 AddLeb128(LookupOrInsertGlobal(var, var_type), false);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 DCHECK(binop->right()->IsLiteral()); 653 DCHECK(binop->right()->IsLiteral());
583 DCHECK_EQ(0.0, binop->right()->AsLiteral()->raw_value()->AsNumber()); 654 DCHECK_EQ(0.0, binop->right()->AsLiteral()->raw_value()->AsNumber());
584 DCHECK(!binop->right()->AsLiteral()->raw_value()->ContainsDot()); 655 DCHECK(!binop->right()->AsLiteral()->raw_value()->ContainsDot());
585 VisitForeignVariable(false, prop); 656 VisitForeignVariable(false, prop);
586 } else { 657 } else {
587 UNREACHABLE(); 658 UNREACHABLE();
588 } 659 }
589 UnLoadInitFunction(); 660 UnLoadInitFunction();
590 return; 661 return;
591 } 662 }
663 Property* prop = expr->value()->AsProperty();
aseemgarg 2016/02/22 22:15:53 This doesn't seem quite right. Stdlib constants wo
bradnelson 2016/02/23 09:48:54 So unlike foreign globals, stdlib functions come i
664 if (prop != nullptr) {
665 if (TypeOf(expr->value()) != kAstStmt) {
666 // Assume it's part of stdlib.
667 return;
668 }
669 }
592 // TODO(bradnelson): Get rid of this. 670 // TODO(bradnelson): Get rid of this.
593 if (TypeOf(expr->value()) == kAstStmt) { 671 if (TypeOf(expr->value()) == kAstStmt) {
594 Property* prop = expr->value()->AsProperty(); 672 Property* prop = expr->value()->AsProperty();
595 if (prop != nullptr) { 673 if (prop != nullptr) {
596 VariableProxy* vp = prop->obj()->AsVariableProxy(); 674 VariableProxy* vp = prop->obj()->AsVariableProxy();
597 if (vp != nullptr && vp->var()->IsParameter() && 675 if (vp != nullptr && vp->var()->IsParameter() &&
598 vp->var()->index() == 1) { 676 vp->var()->index() == 1) {
599 VariableProxy* target = expr->target()->AsVariableProxy(); 677 VariableProxy* target = expr->target()->AsVariableProxy();
600 if (target->bounds().lower->Is(Type::Function())) { 678 if (target->bounds().lower->Is(Type::Function())) {
601 const AstRawString* name = 679 const AstRawString* name =
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 current_function_builder_->Emit(kExprI32And); 836 current_function_builder_->Emit(kExprI32And);
759 byte code[] = {WASM_I8(~(size - 1))}; 837 byte code[] = {WASM_I8(~(size - 1))};
760 current_function_builder_->EmitCode(code, sizeof(code)); 838 current_function_builder_->EmitCode(code, sizeof(code));
761 RECURSE(Visit(binop->left())); 839 RECURSE(Visit(binop->left()));
762 return; 840 return;
763 } 841 }
764 } 842 }
765 UNREACHABLE(); 843 UNREACHABLE();
766 } 844 }
767 845
846 bool VisitStdlibFunction(Call* call, VariableProxy* expr) {
847 Variable* var = expr->var();
848 AsmTyper::StandardMember standard_object =
849 typer_->VariableAsStandardMember(var);
850 ZoneList<Expression*>* args = call->arguments();
851 LocalType call_type = TypeOf(call);
852 switch (standard_object) {
853 case AsmTyper::kNone: {
854 return false;
855 }
856 case AsmTyper::kMathAcos: {
857 UNREACHABLE();
858 break; // TODO(bradnelson): Implement as external.
859 }
860 case AsmTyper::kMathAsin: {
861 UNREACHABLE();
862 break; // TODO(bradnelson): Implement as external.
863 }
864 case AsmTyper::kMathAtan: {
865 UNREACHABLE();
866 break; // TODO(bradnelson): Implement as external.
867 }
868 case AsmTyper::kMathCos: {
869 UNREACHABLE();
870 break; // TODO(bradnelson): Implement as external.
871 }
872 case AsmTyper::kMathSin: {
873 UNREACHABLE();
874 break; // TODO(bradnelson): Implement as external.
875 }
876 case AsmTyper::kMathTan: {
877 UNREACHABLE();
878 break; // TODO(bradnelson): Implement as external.
879 }
880 case AsmTyper::kMathExp: {
881 UNREACHABLE();
882 break; // TODO(bradnelson): Implement as external.
883 }
884 case AsmTyper::kMathLog: {
885 UNREACHABLE();
886 break; // TODO(bradnelson): Implement as external.
887 }
888 case AsmTyper::kMathCeil: {
889 if (call_type == kAstF32) {
890 current_function_builder_->Emit(kExprF32Ceil);
891 } else if (call_type == kAstF64) {
892 current_function_builder_->Emit(kExprF64Ceil);
893 } else {
894 UNREACHABLE();
895 }
896 break;
897 }
898 case AsmTyper::kMathFloor: {
899 if (call_type == kAstF32) {
900 current_function_builder_->Emit(kExprF32Floor);
901 } else if (call_type == kAstF64) {
902 current_function_builder_->Emit(kExprF64Floor);
903 } else {
904 UNREACHABLE();
905 }
906 break;
907 }
908 case AsmTyper::kMathSqrt: {
909 if (call_type == kAstF32) {
910 current_function_builder_->Emit(kExprF32Sqrt);
911 } else if (call_type == kAstF64) {
912 current_function_builder_->Emit(kExprF64Sqrt);
913 } else {
914 UNREACHABLE();
915 }
916 break;
917 }
918 case AsmTyper::kMathAbs: {
919 // TODO(bradnelson): Handle signed.
920 if (call_type == kAstF32) {
921 current_function_builder_->Emit(kExprF32Abs);
922 } else if (call_type == kAstF64) {
923 current_function_builder_->Emit(kExprF64Abs);
924 } else {
925 UNREACHABLE();
926 }
927 break;
928 }
929 case AsmTyper::kMathMin: {
930 // TODO(bradnelson): Handle signed.
931 // TODO(bradnelson): Change wasm to match Math.min in asm.js mode.
932 if (call_type == kAstF32) {
933 current_function_builder_->Emit(kExprF32Min);
934 } else if (call_type == kAstF64) {
935 current_function_builder_->Emit(kExprF64Min);
936 } else {
937 UNREACHABLE();
938 }
939 break;
940 }
941 case AsmTyper::kMathMax: {
942 // TODO(bradnelson): Handle signed.
943 // TODO(bradnelson): Change wasm to match Math.max in asm.js mode.
944 if (call_type == kAstF32) {
945 current_function_builder_->Emit(kExprF32Max);
946 } else if (call_type == kAstF64) {
947 current_function_builder_->Emit(kExprF64Max);
948 } else {
949 UNREACHABLE();
950 }
951 break;
952 }
953 case AsmTyper::kMathAtan2: {
954 UNREACHABLE();
955 break; // TODO(bradnelson): Implement as external.
956 }
957 case AsmTyper::kMathPow: {
958 UNREACHABLE();
959 break; // TODO(bradnelson): Implement as external.
960 }
961 case AsmTyper::kMathImul: {
962 current_function_builder_->Emit(kExprI32Mul);
963 break;
964 }
965 case AsmTyper::kMathFround: {
966 DCHECK(args->length() == 1);
967 Literal* literal = args->at(0)->AsLiteral();
968 if (literal != NULL) {
969 if (literal->raw_value()->IsNumber()) {
970 float val = static_cast<float>(literal->raw_value()->AsNumber());
971 byte code[] = {WASM_F32(val)};
972 current_function_builder_->EmitCode(code, sizeof(code));
973 return true;
974 }
975 }
976 break;
977 }
978 default: { break; }
979 }
980 // Visit arguments.
aseemgarg 2016/02/22 22:15:53 The visiting of args should be done in the visit c
bradnelson 2016/02/23 09:48:55 So sharing the later call is tricky, as Fround wan
981 for (int i = 0; i < args->length(); ++i) {
982 Expression* arg = args->at(i);
983 Visit(arg);
984 }
985 return true;
986 }
987
768 void VisitCall(Call* expr) { 988 void VisitCall(Call* expr) {
769 Call::CallType call_type = expr->GetCallType(isolate_); 989 Call::CallType call_type = expr->GetCallType(isolate_);
770 switch (call_type) { 990 switch (call_type) {
771 case Call::OTHER_CALL: { 991 case Call::OTHER_CALL: {
772 DCHECK(in_function_); 992 DCHECK(in_function_);
993 VariableProxy* proxy = expr->expression()->AsVariableProxy();
994 if (proxy != NULL) {
995 if (VisitStdlibFunction(expr, proxy)) {
996 return;
997 }
998 }
773 uint16_t index; 999 uint16_t index;
774 VariableProxy* vp = expr->expression()->AsVariableProxy(); 1000 VariableProxy* vp = expr->expression()->AsVariableProxy();
775 if (vp != nullptr && 1001 if (vp != nullptr &&
776 Type::Any()->Is(vp->bounds().lower->AsFunction()->Result())) { 1002 Type::Any()->Is(vp->bounds().lower->AsFunction()->Result())) {
777 LocalType return_type = TypeOf(expr); 1003 LocalType return_type = TypeOf(expr);
778 ZoneList<Expression*>* args = expr->arguments(); 1004 ZoneList<Expression*>* args = expr->arguments();
779 FunctionSig::Builder sig(zone(), return_type == kAstStmt ? 0 : 1, 1005 FunctionSig::Builder sig(zone(), return_type == kAstStmt ? 0 : 1,
780 args->length()); 1006 args->length());
781 if (return_type != kAstStmt) { 1007 if (return_type != kAstStmt) {
782 sig.AddReturn(return_type); 1008 sig.AddReturn(return_type);
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
1259 ZoneHashMap global_variables_; 1485 ZoneHashMap global_variables_;
1260 bool in_function_; 1486 bool in_function_;
1261 bool is_set_op_; 1487 bool is_set_op_;
1262 bool marking_exported; 1488 bool marking_exported;
1263 WasmModuleBuilder* builder_; 1489 WasmModuleBuilder* builder_;
1264 WasmFunctionBuilder* current_function_builder_; 1490 WasmFunctionBuilder* current_function_builder_;
1265 FunctionLiteral* literal_; 1491 FunctionLiteral* literal_;
1266 Isolate* isolate_; 1492 Isolate* isolate_;
1267 Zone* zone_; 1493 Zone* zone_;
1268 Handle<Object> foreign_; 1494 Handle<Object> foreign_;
1495 AsmTyper* typer_;
1269 TypeCache const& cache_; 1496 TypeCache const& cache_;
1270 ZoneVector<std::pair<BreakableStatement*, bool>> breakable_blocks_; 1497 ZoneVector<std::pair<BreakableStatement*, bool>> breakable_blocks_;
1271 int block_size_; 1498 int block_size_;
1272 uint16_t init_function_index_; 1499 uint16_t init_function_index_;
1273 uint32_t next_table_index_; 1500 uint32_t next_table_index_;
1274 ZoneHashMap function_tables_; 1501 ZoneHashMap function_tables_;
1275 ImportedFunctionTable imported_function_table_; 1502 ImportedFunctionTable imported_function_table_;
1276 1503
1277 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 1504 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
1278 1505
1279 private: 1506 private:
1280 DISALLOW_COPY_AND_ASSIGN(AsmWasmBuilderImpl); 1507 DISALLOW_COPY_AND_ASSIGN(AsmWasmBuilderImpl);
1281 }; 1508 };
1282 1509
1283 AsmWasmBuilder::AsmWasmBuilder(Isolate* isolate, Zone* zone, 1510 AsmWasmBuilder::AsmWasmBuilder(Isolate* isolate, Zone* zone,
1284 FunctionLiteral* literal, Handle<Object> foreign) 1511 FunctionLiteral* literal, Handle<Object> foreign,
1285 : isolate_(isolate), zone_(zone), literal_(literal), foreign_(foreign) {} 1512 AsmTyper* typer)
1513 : isolate_(isolate),
1514 zone_(zone),
1515 literal_(literal),
1516 foreign_(foreign),
1517 typer_(typer) {}
1286 1518
1287 // TODO(aseemgarg): probably should take zone (to write wasm to) as input so 1519 // TODO(aseemgarg): probably should take zone (to write wasm to) as input so
1288 // that zone in constructor may be thrown away once wasm module is written. 1520 // that zone in constructor may be thrown away once wasm module is written.
1289 WasmModuleIndex* AsmWasmBuilder::Run() { 1521 WasmModuleIndex* AsmWasmBuilder::Run() {
1290 AsmWasmBuilderImpl impl(isolate_, zone_, literal_, foreign_); 1522 AsmWasmBuilderImpl impl(isolate_, zone_, literal_, foreign_, typer_);
1291 impl.Compile(); 1523 impl.Compile();
1292 WasmModuleWriter* writer = impl.builder_->Build(zone_); 1524 WasmModuleWriter* writer = impl.builder_->Build(zone_);
1293 return writer->WriteTo(zone_); 1525 return writer->WriteTo(zone_);
1294 } 1526 }
1295 } // namespace wasm 1527 } // namespace wasm
1296 } // namespace internal 1528 } // namespace internal
1297 } // namespace v8 1529 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/asm-wasm-builder.h ('k') | src/wasm/wasm-js.cc » ('j') | test/mjsunit/wasm/asm-wasm.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698