-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathruntime.rs
70 lines (62 loc) · 2.25 KB
/
runtime.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use powdr_ast::parsed::asm::{FunctionStatement, MachineStatement, SymbolPath};
use powdr_parser::ParserContext;
use powdr_syscalls::Syscall;
lazy_static::lazy_static! {
static ref INSTRUCTION_DECLARATION_PARSER: powdr_parser::powdr::InstructionDeclarationParser
= powdr_parser::powdr::InstructionDeclarationParser::new();
static ref FUNCTION_STATEMENT_PARSER: powdr_parser::powdr::FunctionStatementParser
= powdr_parser::powdr::FunctionStatementParser::new();
}
pub fn parse_instruction_declaration(input: &str) -> MachineStatement {
let ctx = ParserContext::new(None, input);
INSTRUCTION_DECLARATION_PARSER
.parse(&ctx, input)
.expect("invalid instruction declaration")
}
pub fn parse_function_statement(input: &str) -> FunctionStatement {
let ctx = ParserContext::new(None, input);
FUNCTION_STATEMENT_PARSER
.parse(&ctx, input)
.expect("invalid function statement")
}
#[derive(Clone)]
pub struct SubMachine {
/// Full path to machine (e.g, `path::to::Machine`)
pub path: SymbolPath,
/// Optional alias (`use path::to::Machine as TheAlias;`)
pub alias: Option<String>,
/// Instance declaration name,
pub instance_name: String,
/// Arguments
pub arguments: Vec<String>,
/// Instruction declarations
pub instructions: Vec<MachineStatement>,
}
impl SubMachine {
pub fn import(&self) -> String {
format!(
"use {}{}{};",
self.path,
self.alias.as_deref().map(|_| " as ").unwrap_or_default(),
self.alias.as_deref().unwrap_or_default()
)
}
pub fn declaration(&self) -> String {
let ty = self.alias.as_deref().unwrap_or(self.path.name());
let args = if self.arguments.is_empty() {
"".to_string()
} else {
format!("({})", self.arguments.join(", "))
};
format!("{} {}{};", ty, self.instance_name, args)
}
}
/// Sequence of asm function statements.
///
/// Any of the registers used as input/output to the syscall should be usable without issue.
/// Other registers should be saved/restored from memory, as LLVM doesn't know about their usage here.
#[derive(Clone)]
pub struct SyscallImpl {
pub syscall: Syscall,
pub statements: Vec<String>,
}