-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kclvm-runner): encapsulate dylib generating, linking and exe…
…cuting in kclvm/lib.rs into kclvm-runner Encapsulate dylibs generating and executing in kclvm/lib.rs into kclvm-runner. Add struct "KclvmAssembler" in kclvm-runner/runner.rs to provide method "gen_dylibs" for dylibs generating. Add struct "KclvmLinker" in kclvm-runner/runner.rs to provide method "link_all_dylibs" for dylib linking. Add method "execute" in kclvm-runner/lib.rs to encapsulate dylibs generating(gen_dylib), dylib linking(link_all_dylib) and running(runner.run) together The main purpose of separating the three parts from kclvm/lib.rs and encapsulating them into kclvm-runner is to support reuse in kcl-vet. fix #67
- Loading branch information
zong-zhe
committed
Jun 2, 2022
1 parent
c562c0a
commit 3417980
Showing
8 changed files
with
271 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use indexmap::IndexMap; | ||
use std::{collections::HashMap, path::Path, path::PathBuf, sync::mpsc::channel}; | ||
use threadpool::ThreadPool; | ||
|
||
use kclvm_ast::ast::{self, Program}; | ||
use kclvm_compiler::codegen::{llvm::emit_code, EmitOptions}; | ||
use kclvm_config::cache::{load_pkg_cache, save_pkg_cache, CacheOption}; | ||
use kclvm_sema::resolver::resolve_program; | ||
use kclvm_sema::resolver::scope::ProgramScope; | ||
|
||
use crate::{ | ||
command::Command, | ||
runner::{ExecProgramArgs, KclvmRunner, KclvmRunnerOptions}, | ||
}; | ||
|
||
const LL_FILE: &str = "_a.out"; | ||
|
||
/// Evaluator is used to resolve kcl ast, generate dylibs and execute. | ||
pub struct Evaluator {} | ||
|
||
impl Evaluator { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
/// Take ast.program as input,and resolve ast, generate and link dylib, execute. | ||
pub fn eval( | ||
&self, | ||
mut program: Program, | ||
plugin_agent: u64, | ||
args: &ExecProgramArgs, | ||
) -> Result<String, String> { | ||
// resolve ast | ||
let scope = resolve_program(&mut program); | ||
scope.check_scope_diagnostics(); | ||
|
||
// generate dylibs | ||
let dylib_paths = self.gen_dylibs(program, scope, plugin_agent); | ||
|
||
// link dylibs | ||
let dylib_path = self.link_all_dylibs(dylib_paths, plugin_agent); | ||
|
||
// execute | ||
self.run_dylib(dylib_path, plugin_agent, args) | ||
} | ||
|
||
|
||
|
||
|
||
|
||
/// Execute the dylibs linked by method "link_all_dylibs". | ||
fn run_dylib( | ||
&self, | ||
dylib_path: String, | ||
plugin_agent: u64, | ||
args: &ExecProgramArgs, | ||
) -> Result<String, String> { | ||
let runner = KclvmRunner::new( | ||
dylib_path.as_str(), | ||
Some(KclvmRunnerOptions { | ||
plugin_agent_ptr: plugin_agent, | ||
}), | ||
); | ||
runner.run(&args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
use kclvm_ast::ast::Program; | ||
use kclvm_sema::resolver::scope::ProgramScope; | ||
use runner::{ExecProgramArgs, KclvmAssembler, KclvmLinker, KclvmRunner, KclvmRunnerOptions}; | ||
|
||
pub mod command; | ||
pub mod runner; | ||
|
||
pub fn execute( | ||
program: Program, | ||
scope: ProgramScope, | ||
plugin_agent: u64, | ||
args: &ExecProgramArgs, | ||
) -> Result<String, String> { | ||
// generate dylibs | ||
let dylib_paths = KclvmAssembler::gen_dylibs(program, scope, plugin_agent); | ||
|
||
// link dylibs | ||
let dylib_path = KclvmLinker::link_all_dylibs(dylib_paths, plugin_agent); | ||
|
||
// run | ||
let runner = KclvmRunner::new( | ||
dylib_path.as_str(), | ||
Some(KclvmRunnerOptions { | ||
plugin_agent_ptr: plugin_agent, | ||
}), | ||
); | ||
runner.run(&args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.