Executor constructs an executor.
(optimize bool)
| 157 | |
| 158 | // Executor constructs an executor. |
| 159 | func (m *Module) Executor(optimize bool) (*Executor, error) { |
| 160 | m.dbg.finalize() |
| 161 | |
| 162 | if err := m.Verify(); err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | |
| 166 | opts := llvm.NewMCJITCompilerOptions() |
| 167 | if optimize { |
| 168 | opts.SetMCJITOptimizationLevel(2) |
| 169 | } else { |
| 170 | opts.SetMCJITOptimizationLevel(0) |
| 171 | } |
| 172 | |
| 173 | engine, err := llvm.NewMCJITCompiler(m.llvm, opts) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | |
| 178 | // Check target data is as expected. |
| 179 | m.validateTargetData(engine.TargetData()) |
| 180 | |
| 181 | // Check for unresolved extern symbols. |
| 182 | var unresolved []string |
| 183 | for _, f := range m.funcs { |
| 184 | if f.built || strings.HasPrefix(f.Name, "llvm.") { |
| 185 | continue |
| 186 | } |
| 187 | if linker.ProcAddress(f.Name) == 0 { |
| 188 | unresolved = append(unresolved, fmt.Sprint(f)) |
| 189 | } |
| 190 | } |
| 191 | if len(unresolved) > 0 { |
| 192 | sort.Strings(unresolved) |
| 193 | msg := fmt.Sprintf("Unresolved external functions:\n%v", strings.Join(unresolved, "\n")) |
| 194 | fail(msg) |
| 195 | } |
| 196 | |
| 197 | engine.RunStaticConstructors() |
| 198 | |
| 199 | return &Executor{ |
| 200 | llvm: engine, |
| 201 | funcPtrs: map[string]unsafe.Pointer{}, |
| 202 | }, nil |
| 203 | } |
| 204 | |
| 205 | // FunctionAddress returns the address of the function f. |
| 206 | func (e *Executor) FunctionAddress(f *Function) unsafe.Pointer { |