OLD | NEW |
1 // Copyright 2017 the V8 project authors. All rights reserved. | 1 // Copyright 2017 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/ffi/ffi-compiler.h" | 5 #include "src/ffi/ffi-compiler.h" |
6 #include "src/api.h" | 6 #include "src/api.h" |
7 #include "src/code-factory.h" | 7 #include "src/code-factory.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
(...skipping 17 matching lines...) Expand all Loading... |
28 prev_map, instance_size, in_object_properties, unused_property_fields); | 28 prev_map, instance_size, in_object_properties, unused_property_fields); |
29 context->set_native_function_map(*map); | 29 context->set_native_function_map(*map); |
30 } | 30 } |
31 | 31 |
32 namespace ffi { | 32 namespace ffi { |
33 | 33 |
34 class FFIAssembler : public CodeStubAssembler { | 34 class FFIAssembler : public CodeStubAssembler { |
35 public: | 35 public: |
36 explicit FFIAssembler(CodeAssemblerState* state) : CodeStubAssembler(state) {} | 36 explicit FFIAssembler(CodeAssemblerState* state) : CodeStubAssembler(state) {} |
37 | 37 |
38 Node* ToJS(Node* node, Node* context, MachineType type) { | 38 Node* ToJS(Node* node, Node* context, FFIType type) { |
| 39 switch (type) { |
| 40 case FFIType::kInt32: |
| 41 return ChangeInt32ToTagged(node); |
| 42 } |
39 UNREACHABLE(); | 43 UNREACHABLE(); |
40 // TODO(mattloring): Needs to be implemented. | |
41 return nullptr; | 44 return nullptr; |
42 } | 45 } |
43 | 46 |
44 Node* FromJS(Node* node, Node* context, MachineType type) { | 47 Node* FromJS(Node* node, Node* context, FFIType type) { |
| 48 switch (type) { |
| 49 case FFIType::kInt32: |
| 50 return TruncateTaggedToWord32(context, node); |
| 51 } |
45 UNREACHABLE(); | 52 UNREACHABLE(); |
46 // TODO(mattloring): Needs to be implemented. | |
47 return nullptr; | 53 return nullptr; |
48 } | 54 } |
49 | 55 |
| 56 MachineType FFIToMachineType(FFIType type) { |
| 57 switch (type) { |
| 58 case FFIType::kInt32: |
| 59 return MachineType::Int32(); |
| 60 } |
| 61 UNREACHABLE(); |
| 62 return MachineType::None(); |
| 63 } |
| 64 |
| 65 Signature<MachineType>* FFIToMachineSignature(FFISignature* sig) { |
| 66 Signature<MachineType>::Builder sig_builder(zone(), sig->return_count(), |
| 67 sig->parameter_count()); |
| 68 for (size_t i = 0; i < sig->return_count(); i++) { |
| 69 sig_builder.AddReturn(FFIToMachineType(sig->GetReturn(i))); |
| 70 } |
| 71 for (size_t j = 0; j < sig->parameter_count(); j++) { |
| 72 sig_builder.AddParam(FFIToMachineType(sig->GetParam(j))); |
| 73 } |
| 74 return sig_builder.Build(); |
| 75 } |
| 76 |
50 void GenerateJSToNativeWrapper(NativeFunction* func) { | 77 void GenerateJSToNativeWrapper(NativeFunction* func) { |
51 int params = static_cast<int>(func->sig->parameter_count()); | 78 int params = static_cast<int>(func->sig->parameter_count()); |
52 int returns = static_cast<int>(func->sig->return_count()); | 79 int returns = static_cast<int>(func->sig->return_count()); |
53 ApiFunction api_func(func->start); | 80 ApiFunction api_func(func->start); |
54 ExternalReference ref(&api_func, ExternalReference::DIRECT_API_CALL, | 81 ExternalReference ref(&api_func, ExternalReference::BUILTIN_CALL, |
55 isolate()); | 82 isolate()); |
56 | 83 |
57 Node* context_param = GetJSContextParameter(); | 84 Node* context_param = GetJSContextParameter(); |
58 | 85 |
59 Node** inputs = zone()->NewArray<Node*>(params + 1); | 86 Node** inputs = zone()->NewArray<Node*>(params + 1); |
60 int input_count = 0; | 87 int input_count = 0; |
61 inputs[input_count++] = ExternalConstant(ref); | 88 inputs[input_count++] = ExternalConstant(ref); |
62 for (int i = 0; i < params; i++) { | 89 for (int i = 0; i < params; i++) { |
63 inputs[input_count++] = | 90 inputs[input_count++] = |
64 FromJS(Parameter(i), context_param, func->sig->GetParam(i)); | 91 FromJS(Parameter(i), context_param, func->sig->GetParam(i)); |
65 } | 92 } |
66 | 93 |
67 Node* call = CallCFunctionN(func->sig, input_count, inputs); | 94 Node* call = |
| 95 CallCFunctionN(FFIToMachineSignature(func->sig), input_count, inputs); |
68 Node* return_val = UndefinedConstant(); | 96 Node* return_val = UndefinedConstant(); |
69 if (returns == 1) { | 97 if (returns == 1) { |
70 return_val = ToJS(call, context_param, func->sig->GetReturn()); | 98 return_val = ToJS(call, context_param, func->sig->GetReturn()); |
71 } | 99 } |
72 Return(return_val); | 100 Return(return_val); |
73 } | 101 } |
74 }; | 102 }; |
75 | 103 |
76 Handle<JSFunction> CompileJSToNativeWrapper(Isolate* isolate, | 104 Handle<JSFunction> CompileJSToNativeWrapper(Isolate* isolate, |
77 Handle<String> name, | 105 Handle<String> name, |
78 NativeFunction func) { | 106 NativeFunction func) { |
79 int params = static_cast<int>(func.sig->parameter_count()); | 107 int params = static_cast<int>(func.sig->parameter_count()); |
80 Zone zone(isolate->allocator(), ZONE_NAME); | 108 Zone zone(isolate->allocator(), ZONE_NAME); |
81 CodeAssemblerState state(isolate, &zone, params, | 109 CodeAssemblerState state(isolate, &zone, params, |
82 Code::ComputeFlags(Code::FUNCTION), "js-to-native"); | 110 Code::ComputeFlags(Code::BUILTIN), "js-to-native"); |
83 FFIAssembler assembler(&state); | 111 FFIAssembler assembler(&state); |
84 assembler.GenerateJSToNativeWrapper(&func); | 112 assembler.GenerateJSToNativeWrapper(&func); |
85 Handle<Code> code = assembler.GenerateCode(&state); | 113 Handle<Code> code = assembler.GenerateCode(&state); |
86 | 114 |
87 Handle<SharedFunctionInfo> shared = | 115 Handle<SharedFunctionInfo> shared = |
88 isolate->factory()->NewSharedFunctionInfo(name, code, false); | 116 isolate->factory()->NewSharedFunctionInfo(name, code, false); |
89 shared->set_length(params); | 117 shared->set_length(params); |
90 shared->set_internal_formal_parameter_count(params); | 118 shared->set_internal_formal_parameter_count(params); |
91 Handle<JSFunction> function = isolate->factory()->NewFunction( | 119 Handle<JSFunction> function = isolate->factory()->NewFunction( |
92 isolate->native_function_map(), name, code); | 120 isolate->native_function_map(), name, code); |
93 function->set_shared(*shared); | 121 function->set_shared(*shared); |
94 return function; | 122 return function; |
95 } | 123 } |
96 | 124 |
97 } // namespace ffi | 125 } // namespace ffi |
98 } // namespace internal | 126 } // namespace internal |
99 } // namespace v8 | 127 } // namespace v8 |
OLD | NEW |