1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
3  *
4  * Copyright 2015 Mozilla Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef asmjs_wasm_ion_compile_h
20 #define asmjs_wasm_ion_compile_h
21 
22 #include "asmjs/AsmJSFrameIterator.h"
23 #include "asmjs/WasmCompileArgs.h"
24 #include "asmjs/WasmIR.h"
25 #include "jit/MacroAssembler.h"
26 
27 namespace js {
28 namespace wasm {
29 
30 class FunctionCompileResults
31 {
32     jit::TempAllocator alloc_;
33     jit::MacroAssembler masm_;
34     AsmJSFunctionOffsets offsets_;
35     unsigned compileTime_;
36 
37     FunctionCompileResults(const FunctionCompileResults&) = delete;
38     FunctionCompileResults& operator=(const FunctionCompileResults&) = delete;
39 
40   public:
FunctionCompileResults(LifoAlloc & lifo)41     explicit FunctionCompileResults(LifoAlloc& lifo)
42       : alloc_(&lifo),
43         masm_(jit::MacroAssembler::AsmJSToken(), &alloc_),
44         compileTime_(0)
45     {}
46 
alloc()47     jit::TempAllocator& alloc() { return alloc_; }
masm()48     jit::MacroAssembler& masm() { return masm_; }
49 
offsets()50     AsmJSFunctionOffsets& offsets() { return offsets_; }
offsets()51     const AsmJSFunctionOffsets& offsets() const { return offsets_; }
52 
setCompileTime(unsigned t)53     void setCompileTime(unsigned t) { MOZ_ASSERT(!compileTime_); compileTime_ = t; }
compileTime()54     unsigned compileTime() const { return compileTime_; }
55 };
56 
57 class CompileTask
58 {
59     LifoAlloc lifo_;
60     const CompileArgs args_;
61     const FuncIR* func_;
62     mozilla::Maybe<FunctionCompileResults> results_;
63 
64     CompileTask(const CompileTask&) = delete;
65     CompileTask& operator=(const CompileTask&) = delete;
66 
67   public:
CompileTask(size_t defaultChunkSize,CompileArgs args)68     CompileTask(size_t defaultChunkSize, CompileArgs args)
69       : lifo_(defaultChunkSize),
70         args_(args),
71         func_(nullptr)
72     {}
lifo()73     LifoAlloc& lifo() {
74         return lifo_;
75     }
args()76     CompileArgs args() const {
77         return args_;
78     }
init(const FuncIR & func)79     void init(const FuncIR& func) {
80         func_ = &func;
81         results_.emplace(lifo_);
82     }
func()83     const FuncIR& func() const {
84         MOZ_ASSERT(func_);
85         return *func_;
86     }
results()87     FunctionCompileResults& results() {
88         return *results_;
89     }
reset()90     void reset() {
91         func_ = nullptr;
92         results_.reset();
93         lifo_.releaseAll();
94     }
95 };
96 
97 bool
98 CompileFunction(CompileTask* task);
99 
100 } // namespace wasm
101 } // namespace js
102 
103 #endif // asmjs_wasm_ion_compile_h
104