-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathlib.rs
238 lines (207 loc) · 6 KB
/
lib.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) 2020 Ghaith Hachem and Mathias Rieder
//!A St&ructured Text LLVM Frontent
//!
//! RuSTy is an [`ST`] Compiler using LLVM
//!
//! # Features
//! ## Standard language support
//! Most of the [`IEC61131-3`] standard for ST and general programing is supported.
//! ## Native compilation
//! A (currently) single ST files into object code using LLVM.
//! A compiled object can be linked statically or dynamically linked
//! with other programs using standard compiler linkers (ld, clang, gcc)
//! ## IR Output
//! An [`IR`] file can be generated from any given ST file in order to examin the generated LLVM IR code.
//! For a usage guide refer to the [User Documentation](../../)
//!
//! [`ST`]: https://en.wikipedia.org/wiki/Structured_text
//! [`IEC61131-3`]: https://en.wikipedia.org/wiki/IEC_61131-3
//! [`IR`]: https://llvm.org/docs/LangRef.html
use std::convert::Infallible;
use std::str::FromStr;
use clap::clap_derive::ArgEnum;
use plc_index::ErrorFormat as IndexErrorFormat;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use inkwell::targets::{self, TargetMachine, TargetTriple};
#[cfg(test)]
use validation::Validator;
pub mod builtins;
pub mod codegen;
mod datalayout;
pub mod expression_path;
pub mod hardware_binding;
pub mod index;
pub mod lexer;
pub mod linker;
pub mod lowering;
pub mod output;
pub mod parser;
pub mod resolver;
mod test_utils;
pub mod typesystem;
pub mod validation;
extern crate shell_words;
pub const DEFAULT_DWARF_VERSION: usize = 5;
pub const DEFAULT_GOT_LAYOUT_FILE: &str = "online_change_got.json";
#[derive(Default, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum Target {
#[default]
System,
Param {
triple: String,
sysroot: Option<String>,
},
}
impl<'de> Deserialize<'de> for Target {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let target = String::deserialize(deserializer)?;
Ok(target.into())
}
}
impl Target {
pub fn new(triple: String, sysroot: Option<String>) -> Target {
Target::Param { triple, sysroot }
}
pub fn with_sysroot(self, sysroot: Option<String>) -> Target {
match self {
Self::Param { triple, .. } => Target::Param { triple, sysroot },
_ => self,
}
}
pub fn get_target_triple(&self) -> TargetTriple {
let res = match self {
Target::System => TargetMachine::get_default_triple(),
Target::Param { triple, .. } => TargetTriple::create(triple),
};
targets::TargetMachine::normalize_triple(&res)
}
pub fn try_get_name(&self) -> Option<&str> {
match self {
Target::System => None,
Target::Param { triple, .. } => Some(triple.as_str()),
}
}
pub fn get_sysroot(&self) -> Option<&str> {
match self {
Target::Param { sysroot, .. } => sysroot.as_deref(),
_ => None,
}
}
pub fn append_to(&self, location: &Path) -> PathBuf {
match self {
Target::System => location.to_path_buf(),
Target::Param { triple, .. } => location.join(triple),
}
}
}
impl<T> From<T> for Target
where
T: core::ops::Deref<Target = str>,
{
fn from(it: T) -> Self {
Target::new(it.to_string(), None)
}
}
impl FromStr for Target {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Target::from(s))
}
}
#[derive(PartialEq, Eq, Debug, Clone, Copy, ArgEnum)]
pub enum ConfigFormat {
JSON,
TOML,
}
impl FromStr for ConfigFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"json" => Ok(ConfigFormat::JSON),
"toml" => Ok(ConfigFormat::TOML),
_ => Err(format!("Invalid option {s}")),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum, Serialize, Deserialize, Default)]
pub enum ErrorFormat {
#[default]
Rich,
Clang,
None,
}
impl From<ErrorFormat> for IndexErrorFormat {
fn from(value: ErrorFormat) -> Self {
match value {
ErrorFormat::Rich => IndexErrorFormat::Rich,
ErrorFormat::Clang => IndexErrorFormat::Clang,
ErrorFormat::None => IndexErrorFormat::Null,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum Threads {
Full,
Fix(usize),
#[default]
None,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Serialize, Deserialize, Default)]
pub enum OptimizationLevel {
None,
Less,
#[default]
Default,
Aggressive,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DebugLevel {
#[default]
None,
VariablesOnly(usize),
Full(usize),
}
#[derive(Debug, Clone, PartialEq)]
pub enum OnlineChange {
Enabled { file_name: String, format: ConfigFormat },
Disabled,
}
impl OnlineChange {
pub fn is_enabled(&self) -> bool {
matches!(self, OnlineChange::Enabled { .. })
}
}
impl From<OptimizationLevel> for inkwell::OptimizationLevel {
fn from(val: OptimizationLevel) -> Self {
match val {
OptimizationLevel::None => inkwell::OptimizationLevel::None,
OptimizationLevel::Less => inkwell::OptimizationLevel::Less,
OptimizationLevel::Default => inkwell::OptimizationLevel::Default,
OptimizationLevel::Aggressive => inkwell::OptimizationLevel::Aggressive,
}
}
}
impl OptimizationLevel {
fn opt_params(&self) -> &str {
match self {
OptimizationLevel::None => "default<O0>",
OptimizationLevel::Less => "default<O1>",
OptimizationLevel::Default => "default<O2>",
OptimizationLevel::Aggressive => "default<O3>",
}
}
fn is_optimized(&self) -> bool {
!matches!(self, OptimizationLevel::None)
}
}
#[macro_use]
#[cfg(test)]
extern crate pretty_assertions;
#[cfg(test)]
mod tests {
mod adr;
}